🟣 3.1 クラスコンポーネントとは? → @Component, @Prop, data, computed, methods


✅ 【要点】

🎯 Vue 2.7 では「オプションAPI」でも「Composition API」でも「クラスコンポーネント」でも書ける → そのうちの1つの書き方


📦 【最小のクラスコンポーネントの例】

<!-- HelloWorld.vue -->
<template>
  <div>
    <p>{{ message }}</p>
  </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';

@Component
export default class HelloWorld extends Vue {
  message: string = 'Hello Vue Class!';
}
</script>

部分 役割
@Component このクラスが Vue コンポーネントだと教える
extends Vue Vue の機能を使えるようにする
message データ(data 相当)

🎯 @Prop → 親からデータを受け取る(props)

<template>
  <p>Hello, {{ name }}</p>
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator';

@Component
export default class HelloWorld extends Vue {
  @Prop({ default: 'Guest' }) name!: string;
}
</script>

書き方 役割
@Prop() 親からもらうデータ(props)を受け取る
name!: string 型も明示できる(TypeScript の恩恵)
{ default: 'Guest' } デフォルト値を設定できる

⚙️ data → クラスプロパティで書く

message: string = 'Hello Vue Class!';

オプションAPI (data()) クラスコンポーネント (message: string = ...)
data() { return { ... } } message: string = 'Hello'