Vue js,document.title 类型 'undefined' 不可分配给类型 'string',打字稿
Vue js, document.title type 'undefined' is not assignable to type 'string', Typescript
我想用 vue 路由器做 beforeenter 但是,我收到这个错误
TS2322:类型 'string | symbol | null | undefined' 不可分配给类型 'string'。
类型 'undefined' 不可分配给类型 'string'.
17 | beforeEnter(to, from, next) {
18 | // console.log(to)
19 | document.title = to.name
| ^^^^^^^^^^^^^^
20 | console.log(document.title)
21 | }
22 | }
我不知道怎么解决
我假设 to.name 没有定义。尝试将其更改为“测试”或其他内容。我不了解 VueJS,所以可能是其他原因造成的。
试试这个,因为如果你的路由名称是字符串,就不会发生这种情况
beforeEnter(to, from, next)
{
// console.log(to)
if(typeof(to.name) === 'string'){
document.title = to.name;
}
console.log(document.title)
}
我想用 vue 路由器做 beforeenter 但是,我收到这个错误
TS2322:类型 'string | symbol | null | undefined' 不可分配给类型 'string'。 类型 'undefined' 不可分配给类型 'string'.
17 | beforeEnter(to, from, next) {
18 | // console.log(to)
19 | document.title = to.name
| ^^^^^^^^^^^^^^
20 | console.log(document.title)
21 | }
22 | }
我不知道怎么解决
我假设 to.name 没有定义。尝试将其更改为“测试”或其他内容。我不了解 VueJS,所以可能是其他原因造成的。
试试这个,因为如果你的路由名称是字符串,就不会发生这种情况
beforeEnter(to, from, next)
{
// console.log(to)
if(typeof(to.name) === 'string'){
document.title = to.name;
}
console.log(document.title)
}