/* Layout */ import Layout from '@/layout/index.vue' import { ADMIN, DISTRIBUTOR } from…
- User
/* Layout */ import Layout from '@/layout/index.vue' import { ADMIN, DISTRIBUTOR } from '@/api/common' const routerPush = VueRouter.prototype.push VueRouter.prototype.push = function push(location) { return routerPush.call(this, location).catch((error) => error) // Property 'catch' does not exist on type 'void' } 这个 ts 类型报错问题怎么处理
Vue.use(VueRouter)
Assistant
要解决这个问题,需要在
VueRouter.prototype.push函数中添加对catch的类型注解。import Layout from '@/layout/index.vue' import { ADMIN, DISTRIBUTOR } from '@/api/common' const routerPush = VueRouter.prototype.push VueRouter.prototype.push = function push(location): Promise<void> { return routerPush.call(this, location).catch((error) => error) } Vue.use(VueRouter)这样,TypeScript 编译器就会知道
routerPush.call(this, location)函数可能会抛出一个错误,并会正确地将push函数的返回值类型标记为Promise<void>。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.