javascript - Vue Router Navigation Guard 动态导入
问题描述
我想将组件文件中的数据动态导入路由器文件,并根据导入数据的值允许。next()
在App.vue中,this.$router.push({name: "Dashboard"})
当数据 authenticated: false
更改为true
. 我用 触发它watch
。
问题是,即使动态导入,路由器文件也总是会收到原始值。false
应用程序.vue
watch: {
authenticated(){
console.log(this.authenticated) //Outputs 'true'
this.$router.push({name: 'Dashboard'}); //Triggers routing
}
}
路由器文件(index.js)
{
path: '/dashboard',
name: 'Dashboard',
component: Dashboard,
beforeEnter(to, from, next){
(async ()=>{
const mod = await import('../view/App.vue'); //Dynamic import
let result = mod.default.data().auth; //Access 'authenticated' value from App.vue
console.log(result); //Output is 'false', but it should be 'true'
result ? next() : next({name: 'Login'});
})()
}
}
我也尝试了许多不同的async
方法,但没有任何效果。
解决方案
In-Component Guard
在您App.vue
指定的此处使用并从router
文件中删除身份验证登录:
beforeRouteLeave(to, from, next) {
if (to.name === 'Dashboard' && this.authenticated) {
next();
}
else {
next({ name: 'Login' });
}
}
推荐阅读
- dns - Pod 不通过 ingress 解析服务的域名
- laravel - Redis缓存标签刷新内存泄漏
- php - PHP - 两个服务器登录:localhost / localhost:3306 - 控制用户无权访问
- translation - 如何只翻译 Hugo 中的一部分页面?
- php - 在 AWS Lambda 上传和转换文件
- delphi - 有没有办法在 Delphi 7 中获取旧样式(Borland Pascal)对象实例的类名?
- php - jquery 获取 wordpress php
- javascript - TS/JS 用 for 循环中断迭代函数
- postgresql - 如何在 PostgreSQL 的文本列中检测除英语以外的其他语言(非拉丁语)?
- linux - 如何设置当前日期+ 14天的cron表达式