Vue.js vue属性装饰器数据字段声明

时间:2018-12-18 20:02:32

标签: typescript vue.js vuejs2

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

我想念什么?

1 个答案:

答案 0 :(得分: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";
    }
  ];

提示,尽量避免使用任何东西,您正在使用打字稿,请使用键入的对象