2 回答

TA貢獻(xiàn)1864條經(jīng)驗 獲得超2個贊
前段時間,我在一個 Vue.JS 項目中創(chuàng)建了類似的行為。這是我的代碼,也許它可以幫助您找到錯誤。
// 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貢獻(xiàn)1803條經(jīng)驗 獲得超3個贊
在這一點(diǎn)上,我認(rèn)為沒有人有興趣回答我的問題,所以我將只發(fā)布我所做的解決方法:
在應(yīng)用程序的 404 頁面中,注釋掉整個模板內(nèi)容(因此,如果用戶連接速度慢并被重定向到 404,他將看不到任何內(nèi)容)。我還在之前創(chuàng)建的生命周期掛鉤中添加了一個重定向:
beforeCreate() { this.$router.push({ name: "login" }); },
這樣,用戶會自動重定向到登錄名而不會出現(xiàn)任何問題。
添加回答
舉報