vue-class-component提供了mixinshelper函數,以類樣式的方式使用mixins。通過使用mixins幫助程序,TypeScript可以推斷mixin類型並在組件類型上繼承它們。
聲明mixin的示例:
// mixin.js import Vue from 'vue' import Component from 'vue-class-component' // You can declare a mixin as the same style as components. @Component export default class MyMixin extends Vue { mixinValue = 'Hello' }
使用mixin的示例:
import Component, { mixins } from 'vue-class-component'
import MyMixin from './mixin.js'
// Use `mixins` helper function instead of `Vue`.
// `mixins` can receive any number of arguments.
@Component
export class MyComp extends mixins(MyMixin) {
created () {
console.log(this.mixinValue) // -> Hello
}
}
