【发布时间】:2022-01-16 23:21:30
【问题描述】:
我有一个带有 Typescript 和 beforeRouteLeave 钩子的 Nuxt 类组件,如下所示:
import { Component, Vue } from "nuxt-property-decorator"
@Component
export default class MyComponent extends Vue {
beforeRouteLeave (_to, _from, next) {
this.foo()
next()
}
foo () { console.log("navigation guard triggered") }
}
这会引发 TS 错误,因为 _to, _from, next implicitly have an any type。所以我尝试将方法输入为 Vue 路由器 NavigationGuard,如下所示:
import { Component, Vue } from "nuxt-property-decorator"
import type { NavigationGuard } from "vue-router"
@Component
export default class MyComponent extends Vue {
beforeRouteLeave: NavigationGuard = function (this: MyComponent, to, _from, next) { // without typing `this` the next line throws an error
this.foo()
next()
}
foo () { console.log("navigation guard triggered") }
}
TS 错误已消失,但未在应用中触发挂钩。我做错了什么?
【问题讨论】:
标签: typescript vue.js nuxt.js