vue -класс-компонент & Mixins - PullRequest
0 голосов
/ 21 января 2020

Я использую vue -class-компонент с TypeScript для моего Vue проекта. У меня есть компонент и Mixin для него:

// MyComp.vue
import Component, { mixins } from 'vue-class-component'
import MyMixin from './mixin.ts'

@Component
export class MyComp extends mixins(MyMixin) {
  compValue: string = 'Hello';
}


// mixin.ts
import Vue from 'vue'
import Component from 'vue-class-component'

@Component
export default class MyMixin extends Vue {
  created() {
    console.log(this.compValue); // TS2339: Property 'compValue' does not exist on type 'MyMixin'.
  }
}

Он работает, но TypeScript жалуется на отсутствие свойства 'compValue'. Как решить эту проблему?

1 Ответ

0 голосов
/ 21 января 2020

Ошибка возникает из-за того, что compValue не существует в MyMixin. В случае, если это абстрактный класс, который никогда не создается сам по себе и гарантируется, что свойство есть, его можно объявить:

export default class MyMixin extends Vue {
  compValue: string;
  created() {
    console.log(this.compValue);
  }
}
...