Typescript Vue类型脚本插件
我正在编写一个vue记录器插件Typescript Vue类型脚本插件,typescript,vue.js,Typescript,Vue.js,我正在编写一个vue记录器插件 export default new (class NestLogger { public install(Vue: any) { Vue.prototype.$log = this; } error(text: string) { console.log(text) } })(); 主要是 import logger from "./plugins/logger"; Vue.use(logger); 但在组件中,我无法引
export default new (class NestLogger {
public install(Vue: any) {
Vue.prototype.$log = this;
}
error(text: string) {
console.log(text)
}
})();
主要是
import logger from "./plugins/logger";
Vue.use(logger);
但在组件中,我无法引用此。$log-为什么
在Login.vue中
this.$log.error("bla");
错误:属性$log在类型Login上不存在
您需要在.d.ts
文件中包含$log
的类型定义:
//src/my-log-plugin.d.ts
从“Vue”导入Vue
声明模块“vue/types/vue”{
接口Vue{
$log:{
错误:(消息:字符串)=>void
}
}
}
是的,该解决方案是正确的,但对我有效的方法是将上述代码包含在我的log.ts
文件中,而不是将其拆分为.d.ts
文件。如果添加.d.ts
文件,请确保该文件位于tsconfig.json
中为tsc
(和Intellisense)配置的源路径中,以查找该文件。将其放入src/
适用于Vue CLI项目。