LoginSignup
4
1

More than 5 years have passed since last update.

vue(nuxt) + TypeScriptで、propsにcustom classの型を指定する

Posted at

vue-class-componentvue-property-decoratorを使っていて、props等をクラス名で型指定したい場合。

下記の様なクラスがあったとして、

person.ts
export class Person {
  firstName: string;
  lastName: string;
}

下記の様に、componentでpropsのtypeをクラス名で縛ると、実行時エラー(ビルドは通るがブラウザ上でエラー)になる。

pattern1(vue-class-component)
@Component({
  props: {
    someone: Person
  }
})
pattern2(vue-property-decorator)
export default class SomeComponent extends Vue {
  @Prop() someone: Person;
}

下記の様な指定をすれば問題なく動く。

SomeComponent.vue
<template>
  <div/>
</template>

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

@Component
export default class SomeComponent extends Vue {
  @Prop({ type: Object }) someone: Person;
}
</script>

これって補完が効く以外に意味あるのかわからないけど、とりあえず。

4
1
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
4
1