2 回答

TA贡献1864条经验 获得超2个赞
前段时间,我在一个 Vue.JS 项目中创建了类似的行为。这是我的代码,也许它可以帮助您找到错误。
// checks if user is authenticated before displaying the page
// if not, reroutes to the login page
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
if (store.getters.isAuthenticated) {
next();
return;
}
next('/login');
} else {
next();
}
});
元字段定义是否需要身份验证。在下面的代码片段中,您可以看到它是如何使用的。
const routes = [
{
path: '/',
component: DashboardLayout,
redirect: '/login',
children: [
{
path: 'dashboard',
name: 'Dashboard',
component: Dashboard,
meta: {
requiresAuth: true,
},
}
]
}
];
(希望括号是正确的)

TA贡献1803条经验 获得超3个赞
在这一点上,我认为没有人有兴趣回答我的问题,所以我将只发布我所做的解决方法:
在应用程序的 404 页面中,注释掉整个模板内容(因此,如果用户连接速度慢并被重定向到 404,他将看不到任何内容)。我还在之前创建的生命周期挂钩中添加了一个重定向:
beforeCreate() { this.$router.push({ name: "login" }); },
这样,用户会自动重定向到登录名而不会出现任何问题。
添加回答
举报