我正在尝试通过 访问 vue-router 中的会话this.a.app,但它弹出 1 个错误。在我调试之后,我发现它this是未定义的。这是我用来显示的代码this。
我曾尝试使用function(to, from, next) {...}而不是 es6 语法,但this仍然未定义。
router.beforeEach((to, from, next) => {
if (!to.meta.isPublic){
// eslint-disable-next-line
console.log(this)
}
next()
})
Run Code Online (Sandbox Code Playgroud)
是否需要进行任何配置才能this在 vue-router 中访问?
是的,您可以在路由器 beforeEach 挂钩内使用访问“this”,但以不同的方式。路由器具有插入的根实例
用户 router.app 指向“this”(根 Vue 实例)
router.beforeEach((to, from, next) => {
if (!to.meta.isPublic){
// eslint-disable-next-line
console.log(router.app)
}
next()
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
961 次 |
| 最近记录: |