vue如何實現(xiàn)未登錄不能訪問某些頁面
更新時間:2024年03月12日 10:05:54 作者:iiiilooaixuud
這篇文章主要介紹了vue如何實現(xiàn)未登錄不能訪問某些頁面問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
vue實現(xiàn)未登錄不能訪問某些頁面
1.在需要攔截的頁面路由加上
meta: { requireAuth: true }2.在main.js加上判斷即可
router.beforeEach((to, from, next) => {
if (to.meta.requireAuth) { // 需要權(quán)限
//判斷當(dāng)前是否擁有權(quán)限
if (getUserInfo().user_sub) {
next();
} else { // 無權(quán),跳轉(zhuǎn)登錄
mgr.signinRedirect();
}
} else { // 不需要權(quán)限,直接訪問
next();
}
})具體代碼:


vue某些頁面要登錄之后才能訪問,且登錄之后跳轉(zhuǎn)到之前的頁面
// router文件夾下的 index.js
import Vue from "vue";
import Router from "vue-router";
import routes from "./routes"; // 配置的路徑
Vue.use(Router);
const router = new Router({
routes,
scrollBehavior
// mode: 'history'
});
const router = new Router({
routes,
scrollBehavior
// mode: 'history'
});
router.beforeEach((to, from, next) => {
if (to.matched.some(route => route.meta && route.meta.requiresAuth)) {
if (!this.$storage.getStorage("userInfo")) { // 沒有登錄信息跳轉(zhuǎn)到登錄頁
next({
path: "/login",
query: { redirect: to.fullPath } // 'to.fullPath'跳轉(zhuǎn)到登錄之前頁面的路徑
});
} else {
next();
}
} else {
next();
}
});
export default router;
在登陸頁面( login.vue里 )
<template>
<div class="login-container">
<van-form @submit="onSubmit">
<van-field v-model="username" label="用戶名" placeholder="用戶名" required />
<van-field v-model="password" type="password" label="密碼" placeholder="密碼" required />
<div style="margin: 40px 0 0 0;">
<van-button round block type="info" size="small" native-type="submit">提交</van-button>
</div>
</van-form>
</div>
</template>
<script>
import { webLogin } from '@/apis/apis'
export default {
data() {
return {
username: '',
password: '',
}
},
created() { },
methods: {
onSubmit() {
if (!this.username) {
this.$notify({ type: "danger", message: "請?zhí)顚懹脩裘? });
return;
} else if (!this.password) {
this.$notify({ type: "danger", message: "請?zhí)顚懨艽a" });
return;
}
let params = {
username: this.username,
password: this.password
}
webLogin(params).then(res => {
if (res.code == 0) {
this.$notify({ type: 'success', message: '恭喜,登錄成功!' });
// this.$router.push('/user');
this.$storage.setStorage('userInfo', res.data);
setTimeout(() => {
let path = '/user'; // '/user' 為個人中心頁面
if (this.$route.query.redirect) {
path = this.$route.query.redirect // 跳到之前的頁面
}
this.$router.push({
path: path
});
}, 1000)
} else {
this.$notify(res.message)
}
})
}
}
}
</script>

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何用Vite構(gòu)建工具快速創(chuàng)建Vue項目
Vite是一個web開發(fā)構(gòu)建工具,由于其原生?ES?模塊導(dǎo)入方法,它允許快速提供代碼,下面這篇文章主要給大家介紹了關(guān)于如何用Vite構(gòu)建工具快速創(chuàng)建Vue項目的相關(guān)資料,需要的朋友可以參考下2022-05-05
使用Vue.js實現(xiàn)數(shù)據(jù)的雙向綁定
在Vue.js中,雙向數(shù)據(jù)綁定是一項非常強(qiáng)大的功能,它能夠使數(shù)據(jù)和視圖之間保持同步,讓開發(fā)者更加方便地操作數(shù)據(jù),在本文中,我們將介紹如何用Vue.js實現(xiàn)數(shù)據(jù)的雙向綁定,需要的朋友可以參考下2023-04-04
Vue的hover/click事件如何動態(tài)改變顏色和背景色
這篇文章主要介紹了Vue的hover/click事件如何動態(tài)改變顏色和背景色問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Vue中v-form標(biāo)簽里的:rules作用及定義方法
文章介紹了在Vue項目中使用ElementUI或ElementPlus組件庫時,如何通過`el-form`標(biāo)簽的`:rules`屬性進(jìn)行表單驗證,`:rules`屬性用于定義表單項的驗證規(guī)則,包括必填項、格式校驗、長度限制等,文章還展示了如何定義基本驗證規(guī)則和自定義驗證函數(shù),感興趣的朋友一起看看吧2025-03-03
談?wù)剬ue響應(yīng)式數(shù)據(jù)更新的誤解
本篇文章主要介紹了談?wù)剬ue響應(yīng)式數(shù)據(jù)更新的誤解,深入了解了vue響應(yīng)式數(shù)據(jù),有興趣的可以了解一下2017-08-08

