typescript - Vuejs vue-property-decorator 数据字段声明
问题描述
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
我错过了什么?
解决方案
查看运行示例: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
推荐阅读
- wcf - 尝试添加 WCF Web 服务器引用提供程序失败,因为它尝试为已存在的 DNC 目录创建目录
- arduino - 如何解决我的程序卡住并重复自己的问题?
- google-chrome - 在 chrome 背景选项卡中上传
- python-3.x - 如何使用 selenium Python 单击具有动态 ID 的按钮
- c# - e_driver.register(eventListener) 中“注册”的替代方法是什么?
- javascript - 查找函数的值
- sql - SELECT INTO 在函数内
- r - 在 foreach dopar 循环中更改 data.frame 对象
- amazon-web-services - AWS Sagemaker boto3 - 通过端点预测与在本地运行模型获得不同的预测
- r - 我正在尝试在 squanstrat 中制定一种策略,当 200 SMA 大于股票时买入 QQQ,而在相反时卖出