【发布时间】:2021-08-03 07:35:28
【问题描述】:
我在 Typescript 中有一个 Vuejs 项目。项目编译并运行完美,没有错误。但是 TS linter 是错误的。
我在单个组件文件中使用组件装饰器,如下所示:
//videocard.component.vue
<script lang="ts">
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import { Video } from '../interfaces/video.interface';
@Component
export default class VideoCardComponent extends Vue {
@Prop() readonly video: Video;
@Prop() readonly loading: boolean;
created(){
console.log(this.static_url); // !COMPLAINS HERE!
}
get _video(){
return this.video
}
get _loading(){
return this.loading || false;
}
}
</script>
请注意我是如何尝试注销名为 static_url 的属性的。这是有效的,因为在我的 app.ts 文件中,我正在设置这个属性:
//app.ts
Vue.prototype.static_url = (window as any).static_url;
我增加了类型,所以static_url 是 Vue 上的一个属性。像这样:
// static_url.d.ts
import Vue from 'vue';
declare module 'vue/types/vue' {
interface Vue {
static_url: string;
}
interface VueConstructor {
static_url: string
}
}
Typescript linter 不会抱怨 app.ts 文件中的这个属性,但它会抱怨组件文件中的这个属性。为什么 Typescript 无法识别组件文件中的这个属性?
为了完整起见,这是我的tsconfig.json 文件:
{
"compilerOptions": {
"target": "es5",
"lib": ["esnext", "dom"],
"strict": true,
"module": "es2015",
"noImplicitAny": false,
"moduleResolution": "node",
"experimentalDecorators": true,
"strictPropertyInitialization": false
},
"include": [
"assets/js/video-app/src/**/*.ts",
"assets/js/video-app/src/**/*.d.ts",
]
}
【问题讨论】:
标签: typescript vue.js