SO社区,
我正在尝试在现有的Vue 2应用程序中添加外部身份验证。我遇到的问题是,当外部IdP重定向回Vue应用程序并带有路径/redirect时,Vue Router似乎不会遵守更新后的路径并路由到正确的组件。换句话说,Vue Router会将路由到/组件而不是/redirect组件。
Vue Router设置了mode: 'history'和Webpack设置了historyApiFallback: true,。
https://localhost:8080/并呈现Login组件。https://localhost:8080/redirect/,从而返回到Login组件。Login组件中,mounted()钩子检查if (window.location.pathname === '/redirect') router.push({ path: '/redirect' });/路径,而不是/redirect和其组件。Vue - 2.5.15
路由器视图 - 3.0.0
Webpack - 4.17.1
Webpack 开发服务器 - 3.1.4
webpack.dev.jsdevServer: {
historyApiFallback: true,
router.jsconst router = new VueRouter({
mode: 'history',
routes: [
...
{
path: '/redirect',
component: Redirect,
},
{
path: '/',
redirect: () => {
},
...
router.beforeEach((to, from, next) => {
const user = store.state.user.authorizedUser
const toPath = to.path
if (toPath === '/redirect') return
});
App.vuenew Vue({
el: '#login',
render: h => h(Login),
store,
})
登录.vuemounted() {
if (window.location.pathname === '/redirect') router.push({ path: '/redirect' });
}
请告诉我是否需要提供任何其他细节或代码。提前感谢您的帮助。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
所以,问题似乎出现在顶层组件
Login在路由器启动之前充当交通警察的角色。我不得不编辑Login组件以手动挂载Redirect组件。我不建议这种设置方式。