Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]主应用配置404页面 在路由守卫中调用next()执行 子应用的路径时 直接跳404! #918

Closed
tianliangOrmoon opened this issue Sep 9, 2020 · 3 comments

Comments

@tianliangOrmoon
Copy link

提问之前强烈建立您能先阅读一下《提问的智慧》

What happens?

在主应用中 配置了路由{
path:'*',
name:'404',
component: () =>import('../views/404.vue')
}
我现在访问子应用的路由 在路由守卫中的next()后会直接跳转到404页面

在主应用中 配置了路由{
path:'*',
name:'404',
component: () =>import('../views/404.vue')
}
我现在访问子应用的路由 在路由守卫中的next()后会直接跳转到404页面

相关环境信息

  • qiankun v2.0.24
  • 各类浏览器
  • 操作系统
@gongshun
Copy link
Collaborator

这是正常表现,你不应该写通配符 * ,你可以将 404 页面注册为一个普通路由页面,比如说 /404 ,然后在主项目的路由钩子函数router.beforeEach 里面判断一下,如果既不是主项目路由,也不是子项目,就跳转到 404 页面,伪代码如下:

const childrenPath = ['/app1','/app2'];
router.beforeEach((to, from, next) => {
    if(to.name) { // 有name属性,说明是主项目的路由
        next()
    }
    if(childrenPath.some(item => to.path.includes(item))){
        next()
    }
    next({ name:'404' })
})

@tianliangOrmoon
Copy link
Author

thank you

@ghost
Copy link

ghost commented Nov 19, 2021

const childrenPath = ['/app1','/app2'];
router.beforeEach((to, from, next) => {
if(to.name) { // 有name属性,说明是主项目的路由
return next()
}
if(childrenPath.some(item => to.path.includes(item))){
return next()
}
next({ name:'404' })
})

这段代码应该要加 return ,不然 next() 之后的代码还是会执行到的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants