我使用计算属性在路由URL中获取id参数,如下所示:
export default defineComponent({
computed: {
extractionId() : string {
return this.$route.params.id as string;
},
releves() {
let extractionId = this.extractionId;
return this.$store.getters.getExtractionById(extractionId)
}
},
// ....
但是当我想访问this.extractionId
时,我在releves
中有一个错误,我有这个错误显示:
Property 'extractionId' doesn't exist on type 'CreateComponentPublicInstance<{ [x: `on${string}`]: ((...args: any[]) => any) | undefined; } | { [x: `on${string}`]: ((...args: never) => any) | undefined; }, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 10 more ..., {}>'.
我不能依赖另一个计算属性吗?
错误信息一点帮助都没有。实际上,Typescript并不高兴,因为两个计算属性都必须声明它们的返回类型. 我的releve
属性没有声明它的返回类型,所以我这样做了:
export default defineComponent({
computed: {
extractionId() : string {
return this.$route.params.id as string;
},
releves(): number[] {
let extractionId = this.extractionId;
return this.$store.getters.getExtractionById(extractionId).releves
}
},
我可以走了
裁判:https://vuejs.org/guide/typescript/options-api.html typing-computed-properties