【发布时间】:2018-02-08 09:14:04
【问题描述】:
如果用户已登录,我目前正在尝试仅显示页面。我面临的问题是 requireAuth() 似乎被无数次调用。
使用的代码是:
// Routes
const routes = [
{
path: '/',
component: Dashboard,
beforeEnter: (to, from, next) => {
requireAuth(to, from, next);
},
children: [
{
path: '',
name: 'dashboard',
component: DashboardIndex
}, {
path: '*',
name: '404',
component: NotFound
}
]
}, {
path: '/login',
component: Login,
name: 'login',
},
];
function requireAuth (to, from, next) {
if (!localStorage.token) {
console.log('testing');
next({
path: '/login',
query: { redirect: to.fullPath }
})
} else {
next()
}
}
// Routing logic
let router = new VueRouter({
routes: routes,
mode: 'hash'
});
testing 在我收到错误之前输出了大约 1000 次:
[vue-router] 路由导航期间未捕获的错误: 警告@app.js
app.js RangeError: 超出最大调用堆栈大小
如果!localStorage.token,我如何确保/login 被重定向到?
【问题讨论】:
标签: javascript vue.js vue-router