【问题标题】:Vuejs vue-property-decorator data field declarationVuejs vue-property-decorator 数据字段声明
【发布时间】:2019-05-19 07:06:16
【问题描述】:

Vue 新手。

使用vue-property-decorator & 也尝试过vue-class-component

根据 github (https://github.com/kaorun343/vue-property-decorator),data 对象在类中被明确声明,例如ecosystem

  import {Component, Vue} from 'vue-property-decorator';

  @Component({
     components: {}
  })
  export default class HelloWorld extends Vue {
      ecosystem: [
        {
          text: 'vuetify-loader',
          href: 'https://github.com/vuetifyjs/vuetify-loader'
        },
        {
          text: 'github',
          href: 'https://github.com/vuetifyjs/vuetify'
        },
        {
          text: 'awesome-vuetify',
          href: 'https://github.com/vuetifyjs/awesome-vuetify'
        }
      ] 
  }

而不是

  data() {
    return {
      ecosystem: ['bla', 'bla', 'bla']
    }
  }

但是当我尝试使用它时,得到错误Property or method "ecosystem" is not defined on the instance but referenced during render.

打电话给

  created() {
    console.log(this.ecosystem)
  }

也返回undefined

我错过了什么?

【问题讨论】:

    标签: typescript vue.js vuejs2


    【解决方案1】:

    查看运行示例:https://codesandbox.io/s/y2nop643xv

    像这样声明生态系统:

    ecosystem: any = [
        {
          text: "vuetify-loader";
          href: "https://github.com/vuetifyjs/vuetify-loader";
        },
        {
          text: "github";
          href: "https://github.com/vuetifyjs/vuetify";
        },
        {
          text: "awesome-vuetify";
          href: "https://github.com/vuetifyjs/awesome-vuetify";
        }
      ];
    

    还有一点提示,尽量避免使用 any,你正在使用 typescript,使用 typed objects

    【讨论】:

    • 我的天啊,是的,盲目的个人错误,发生了:D
    猜你喜欢
    • 2020-03-10
    • 1970-01-01
    • 2019-02-27
    • 2018-05-01
    • 2019-09-08
    • 2020-09-15
    • 2019-01-29
    • 2019-12-17
    • 2021-11-01
    相关资源
    最近更新 更多