[英]vue-class-component & Mixins
I use vue-class-component with TypeScript for my Vue project.我将 vue-class-component 与 TypeScript 用于我的 Vue 项目。 I have the component and the Mixin for it:我有它的组件和 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'.
}
}
It works, but TypeScript complains about missing Property 'compValue'.它有效,但 TypeScript 抱怨缺少属性“compValue”。 How to fix this problem?如何解决这个问题?
The error happens because compValue
doesn't exist in MyMixin
.发生错误是因为compValue
中不存在MyMixin
。 In case this is abstract class that is never instantiated on its own and it's guaranteed the property is there, it can be declared:如果这是一个永远不会单独实例化的抽象类,并且保证该属性在那里,则可以声明它:
export default class MyMixin extends Vue {
compValue: string;
created() {
console.log(this.compValue);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.