如何在typescript中重载到vue组件装饰器?
我将vue与typescript装饰器一起使用:如何在typescript中重载到vue组件装饰器?,typescript,vue.js,Typescript,Vue.js,我将vue与typescript装饰器一起使用: 从“Vue属性装饰器”导入{Component,Prop,Vue,Watch}; @组成部分({ 组件:{..}, ... }) ... 我想添加一个属性以传递给装饰器,如下所示: @组件({ 组件:{..}, myprop:。。。 ... }) ... 但是typescript不知道我的新财产。(javascript有),我可以为@Component decorator执行重载吗 如何在typescript中重载到vue组件装饰器 您写道J
从“Vue属性装饰器”导入{Component,Prop,Vue,Watch};
@组成部分({
组件:{..},
...
}) ...
我想添加一个属性以传递给装饰器,如下所示:
@组件({
组件:{..},
myprop:。。。
...
}) ...
但是typescript不知道我的新财产。(javascript有),我可以为@Component decorator执行重载吗
如何在typescript中重载到vue组件装饰器
您写道JavaScript已经知道您的新属性。因此,您只需告诉TypeScript该属性存在。然后编译器将停止抱怨
通过使用,可以告诉TypeScript属性存在。声明合并告诉TypeScript合并两个或多个类型声明的属性
在下面的示例中,我们使用(一种声明合并)告诉编译器有关新属性的信息
import { Component } from "vue-property-decorator";
// Augment the vue module
declare module "vue" {
// Add myprop to the ComponentOptions type
interface ComponentOptions<V> {
myprop: string;
}
}
@Component({
data: {},
myprop: ""
})
export default class MyComponent extends Vue {}
从“vue属性装饰器”导入{Component};
//扩充vue模块
声明模块“vue”{
//将myprop添加到ComponentOptions类型
接口组件选项{
myprop:string;
}
}
@组成部分({
数据:{},
myprop:“
})
导出默认类MyComponent扩展Vue{}
瞧!TypeScript不再抱怨了
为了使代码更有条理,模块扩充可以像这样进入自己的文件:
我想更改VueConstructor选项,但不知道如何更改。所以每次我使用@Component时,我都会放上我的道具,我不想让typescript说错。太好了!!谢谢大家!@肖恩·卢廷你能查一下我的帖子吗?