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" }); },
這樣,用戶會自動重定向到登錄名而不會出現任何問題。
添加回答
舉報