本文重点解决vue框架中pops的ypescip用法问题,希望能够帮助到你
在vue中使用typescript时,需要引入vue-property-decorator库来兼容格式。
javascript写法
Vue.component('blog-post', {
// 在 JavaScript 中是 camelCase 的
props: ['postTitle'],
template: '<h3>{{ postTitle }}</h3>'
})
typescript写法
@Prop({
type: Array,
default: function(): Array<LabelData> {
return [];
}
})
label_list: Array<LabelData> | undefined;
typescript和javascript在用法的区别,主要是需要严格规定label_list的类型。
但是,在vue里面,prop是不能赋初始值的。这个规则和typescript会发生矛盾,因此定义类型需要加undefined,避免typescript转义告警。
在代码中使用label_list时,需要用label_list as Array
的语法,转换成正常的数组格式
vue props
vue-property-decorator
本网刊登的文章均仅代表作者个人观点,并不代表本网立场。文中的论述和观点,敬请读者注意判断。