我已经为此做了大约两个小时了,没有用。我搜索过互联网,尝试过创造条件,等等。
因此,我在VueJS组件中有一个方法,如下所示:
paginate() {
const test = this.$route.query.page;
console.log(test);
this.currPage = parseInt(test);
}
最后一行中的"test“一词有一个红色的下划线,我收到一条错误消息,上面写着:
const测试: LocationQueryValue = LocationQueryValue[] 类型为'LocationQueryValue \ LocationQueryValue[]‘的参数不能分配给'string’类型的参数。 输入'null‘不能分配到输入’string‘..Vetur(2345)
如何在不编辑TypeScript配置文件的情况下解决TypeScript错误?
发布于 2022-05-23 21:23:21
这意味着test
可以是null
。它的类型可能是string | null
,它是一个联合类型。您需要测试它以消除null
的情况。
例如
paginate() {
const test = this.$route.query.page;
console.log(test);
if (typeof test === 'string') {
// `test` has type string here because of the predicate.
this.currPage = parseInt(test);
}
}
请注意,上述解决方案虽然确实正确,但更改了原始代码的含义,如果this.currPage
test
不是字符串,则将保持不变。
https://stackoverflow.com/questions/72354317
复制相似问题