【问题标题】:How to use mixins in Nuxt.js app with nuxt-property-decortor如何通过 nuxt-property-decorator 在 Nuxt.js 应用程序中使用 mixins
【发布时间】:2020-12-05 13:01:31
【问题描述】:

我在最新的项目中使用了 Nuxt.js,语言是 TypeScript。 另外,我正在使用nuxt-property-decorator。 我正在尝试理解以下代码中的“mixins”属性。

mixins.vue ↓

<template>
  <div>    
    <p>{{hello}}</p>
  </div>   
</template>

<script lang="ts">
import { Component ,Vue } from 'nuxt-property-decorator'
import Mixin from "~/mixins/mixin";
@Component({
    mixins:[
        Mixin
    ]
})
export default class extends Vue{
    greeting:string = 'Hello'
}
</script>

mixin.ts↓

import { Vue } from "nuxt-property-decorator";
export default class extends Vue {
    greeting:string = ''
    message:string = 'world'
    get hello(){
        return this.greeting + ' ' + this.message + '!'
    }
}

我在输出中期待"Hello worlds!",但发生错误:

Property or method "hello" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

谁能给我建议?

【问题讨论】:

    标签: javascript typescript vue.js nuxt.js mixins


    【解决方案1】:

    mixin 必须用@Component: 修饰:

    // mixin.ts
    import { Component, Vue } from "nuxt-property-decorator";
    
    @Component ?
    export default class extends Vue {
      //...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-28
      • 2020-12-13
      • 2021-03-09
      • 2021-10-20
      • 1970-01-01
      • 2020-10-18
      • 2018-07-29
      • 2021-05-26
      相关资源
      最近更新 更多