Vue Router 不重定向
Vue Router does not redirect
如果用户已登录,我目前正在尝试只显示页面。我面临的问题是 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] uncaught error during route navigation:
warn @ app.js
app.js RangeError: Maximum call stack size exceeded
如何确保 /login
被重定向到 if !localStorage.token
?
如果您没有 localStorage
令牌,您会将用户重定向到 /login
。
因为这也是一个 Vue 路由,你的 requireAuth 逻辑将再次 运行(因为它对每个路由都是 运行)。这意味着您刚刚创建了一个无限循环,即使用户已经在该页面上,用户也会不断地被重定向到 /login
。
如果您已经在 /login
.
,请不要重定向到 /login
我会把那部分留给你,但如果你明白发生了什么,应该不会那么难。
我遇到了同样的问题,因为相应的错误源都归结为 next()
函数,这是导航到具有 to.path
作为值的路径所必需的。如果您将使用 router.push
或 router.replace
那么可能会被调用无数次,因为调用堆栈最大错误显示。所以简单地使用 next()
让 router
API 做繁琐的工作
我做过这种事,只是方式不同。我在 main.js
文件中处理了所有逻辑。 routes.js
文件包含 -
var routes = [{
path:'/login',
component: Login
},
{
path:'/',
component: dashboard
}]
现在我已经使用 vue-router
API 控制了 main.js 文件中的所有类型的验证,因为我从中得到了帮助 - https://router.vuejs.org/en/api/route-object.html
所以现在 main.js
将包含 -
const checkToken = () => {
if(localStorage.getItem('token') == (null || undefined) ){
console.log('token is not there : ' + localStorage.getItem('token'));
return false;
}
else{
return true
}
}
//then Use `router.push('/login')` as
router.beforeEach((to, from, next) => {
if(to.path == '/') {
if(checkToken()) {
console.log('There is a token, resume. (' + to.path + ')' + 'localstorage token ' + localStorage.getItem("token"));
next();
} else {
console.log('There is no token, redirect to login. (' + to.path + ')');
router.push('/login');
}
}
所以你可以像这样构建结构来控制 main.js
中的所有东西,让 route.js
远离所有东西
如果用户已登录,我目前正在尝试只显示页面。我面临的问题是 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] uncaught error during route navigation: warn @ app.js
app.js RangeError: Maximum call stack size exceeded
如何确保 /login
被重定向到 if !localStorage.token
?
如果您没有 localStorage
令牌,您会将用户重定向到 /login
。
因为这也是一个 Vue 路由,你的 requireAuth 逻辑将再次 运行(因为它对每个路由都是 运行)。这意味着您刚刚创建了一个无限循环,即使用户已经在该页面上,用户也会不断地被重定向到 /login
。
如果您已经在 /login
.
/login
我会把那部分留给你,但如果你明白发生了什么,应该不会那么难。
我遇到了同样的问题,因为相应的错误源都归结为 next()
函数,这是导航到具有 to.path
作为值的路径所必需的。如果您将使用 router.push
或 router.replace
那么可能会被调用无数次,因为调用堆栈最大错误显示。所以简单地使用 next()
让 router
API 做繁琐的工作
我做过这种事,只是方式不同。我在 main.js
文件中处理了所有逻辑。 routes.js
文件包含 -
var routes = [{
path:'/login',
component: Login
},
{
path:'/',
component: dashboard
}]
现在我已经使用 vue-router
API 控制了 main.js 文件中的所有类型的验证,因为我从中得到了帮助 - https://router.vuejs.org/en/api/route-object.html
所以现在 main.js
将包含 -
const checkToken = () => {
if(localStorage.getItem('token') == (null || undefined) ){
console.log('token is not there : ' + localStorage.getItem('token'));
return false;
}
else{
return true
}
}
//then Use `router.push('/login')` as
router.beforeEach((to, from, next) => {
if(to.path == '/') {
if(checkToken()) {
console.log('There is a token, resume. (' + to.path + ')' + 'localstorage token ' + localStorage.getItem("token"));
next();
} else {
console.log('There is no token, redirect to login. (' + to.path + ')');
router.push('/login');
}
}
所以你可以像这样构建结构来控制 main.js
中的所有东西,让 route.js
远离所有东西