简体   繁体   English

vue-class-component & Mixins

[英]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.

相关问题 vue类组件mixins在WebStorm中引发错误? - vue-class-component mixins provoke an error in WebStorm? vue-class-component如何同时使用基类和mixins? - How can both a base class and mixins be used with vue-class-component? Vuelidate 与 Vue 3 + vue-class-component + TypeScript - Vuelidate with Vue 3 + vue-class-component + TypeScript Vue JS - vue-class-component 绑定输入 - Vue JS - vue-class-component binding input VueJS 组件:具有 vue-class-component 的单独文件的代码覆盖率 - VueJS Component: Code coverage for separate file with vue-class-component vue-class-component:如何创建和初始化反射数据 - vue-class-component : how to create and initialize reflective data Vue&TypeScript:vue-property-decorator / vue-class-component的'el'属性? - Vue&TypeScript: 'el' property for vue-property-decorator/vue-class-component? Vue 2.5 vue-class-component使用Vue命名空间,但是它是Type - Vue 2.5 vue-class-component uses Vue namespace but it's a Type vue-class-component + typescript:如何在导入的 function 中使用组件的 class 作为“this”类型? - vue-class-component + typescript: how to use a component's class as a type of "this" in imported function? vue-class-component 语法中 vue-models(因此,“props”)的计算 getter/setter - Computed getter/setter for vue-models (therefore, “props”) in vue-class-component syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM