0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Typescript&VueでのPropsで値を渡す方法

Posted at

子のコンポーネントはこんな感じで書くと割と便利

<template>
// 省略
</template>
<script lang="ts">
import { Vue, Prop } from 'nuxt-property-decorator';
// 省略
export default class Hoge extends Vue {
  @Prop(Number) readonly hogeNum!: number;
  @Prop(String) readonly hogeStr!: string;
  @Prop(Array) readonly hogeAry!: string[]; // 文字列が入る配列の型
  @Prop(Object) readonly hogeObj!: { [key: string]: number }; // 適当な文字列をkeyに数値が入る型
  @Prop({ type: Boolean, default: true }) readonly hogeBool!: boolean; // 何も親コンポーネントから渡されない場合はtrueが入る

  hogeFunc(): void {
    console.log(this.hogeNum); // thisで呼び出す
  }
}
</script>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?