vuejs,this。$ route.query.page,然后键入' null'不能分配给类型' String'
我一直在研究这个问题大约2个小时,没有可用。我已经搜索了Internet,尝试创建条件等。
因此,我在Vuejs组件中有一种方法,看起来像这样:
paginate() {
const test = this.$route.query.page;
console.log(test);
this.currPage = parseInt(test);
}
最后一行的“测试”一词具有红色下划线,并且我收到一条错误消息:
const test:locationqueryvalue | locationQueryValue []
类型'LocationQueryValue的参数| LocationQueryValue []'不可分配给类型'String'的参数。
type'null'不能分配给类型'string'.vetur(2345)
我应该如何在不编辑打字稿配置文件的情况下求解打字稿错误?
I have been working on this for about 2 hours now, no to avail. I have searched the Internet, tried creating a condition, etc.
So I have a method in a VueJS component that looks like this:
paginate() {
const test = this.$route.query.page;
console.log(test);
this.currPage = parseInt(test);
}
The word "test" on the last line has a red underline and I get an error message saying this:
const test: LocationQueryValue | LocationQueryValue[]
Argument of type 'LocationQueryValue | LocationQueryValue[]' is not assignable to parameter of type 'string'.
Type 'null' is not assignable to type 'string'.Vetur(2345)
How should I solve the TypeScript error without editing the TypeScript configuration file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它的意思是
测试
可以是null
。它的类型可能是字符串| null
是联合类型。您需要测试它以消除null
案例。For example
Playground Link
请注意,上面的解决方案虽然确实正确,但会更改原始代码的含义,而如果
test> test
不是字符串。It means
test
can benull
. Its type is likelystring | null
which is a union type. You need test it to eliminate thenull
case.For example
Playground Link
Please note that the above solution, while indeed proper, changes the meaning of the original code, leaving
this.currPage
unchanged iftest
is not a string.不仅是您的变量
test
可以是null
,还可以是String> String
和/或null
null 的数组代码>值。这是最好的情况:
https://www.example.com /?pag = 3
,但从技术上讲也是如此:
,'123',null,''] 。
您可以做的事情:
引用VUE路由器文档。
It is not just that your variable
test
can benull
, it could also be an array ofstring
and/ornull
values.This is the best case scenario:
https://www.example.com/?page=3
But this could technically also be the case:
https://www.example.com/?page=abc&page=123&page&page=
Now your variable would hold
['abc', '123', null, '']
.Something that you could do:
Reference to the Vue Router docs.