javascript - TypeScript 抱怨使用 vue-property-decorator 定义的现有 Vue 组件属性
问题描述
我有一个 Vue 组件,它有一个使用装饰器定义的属性:
import { Component, Vue } from "vue-property-decorator"
@Component({
props: {
myId: String,
},
})
class TestProp extends Vue {
myFunction(obj: any) {
return obj[this.myId] // here tslint complains: "Property 'myId' does not exist on type 'TestProp'."
}
}
我可以通过转换为来避免类型this
错误any
:
myFunction(obj: any) {
return obj[(this as any).myId]
}
但这与其说是解决方案,不如说是一种解决方法。
@Component
我猜编译器不知道装饰器定义的属性?
有任何想法吗?
解决方案
我推荐你使用这个库:https ://github.com/kaorun343/vue-property-decorator
有了这个,你可以在你的组件类中声明你的道具。
例如:
import { Vue, Component, Prop } from 'vue-property-decorator'
@Component
class TestProp extends Vue {
@Prop(String) myId: string!
}
推荐阅读
- javascript - 如何删除对象数组中的特定元素
- android - 带有 setFocus 的 EditText OnKeyListener 无法按预期工作
- stream - 我应该以什么速率采样以使依赖数据流独立?
- sql - 计算postgresql矩阵中的列组合
- ubuntu - Ubuntu 18.4 maas 节点调试失败 lldpd
- symfony - 如果值在另一列中为真,则 NOT NULL
- identityserver4 - Identity Server 4 - 使用刷新的令牌进行身份验证
- php - 在while循环之后限制if语句中的项目数
- assembly - 如何在 MIPS 程序集中将一个字符串复制到另一个字符串
- javascript - 首次加载时不同的 jQuery 过期时间