我遇到了无法使用 next('/login')
的问题。这是我的代码:
*/ all imports that are needed
Vue.use(Router);
const routes = [
{path: '/', component: Home},
{path: '/admin', component: Admin},
{path: '/login', component: Login}]
var route = new Router({
routes: routes,
mode: 'history' });
route.beforeEach((to, from , next) => {
console.log(to.fullPath)
next('/login');
});
new Vue({
el: '#app',
router: route,
template: '<App/>',
components: {
App
}
})
当我只使用 next() 但当我给 next() 函数一个路由时它会卡在无限循环中
最佳答案
如果您已经定向到 '/login'
,则必须阻止调用 next('/login')
。
例如:
route.beforeEach((to, from , next) => {
console.log(to.fullPath)
if (to.path !== '/login') {
next('/login');
} else {
next();
}
});
关于vue.js - Vue-router beforeEach 在使用 next() 时陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45976708/