vue?router權(quán)限管理實(shí)現(xiàn)不同角色顯示不同路由
思路:
- login頁(yè)面登錄時(shí) 加上角色的標(biāo)記,存儲(chǔ)到本地緩存(localstorage)
- 路由js文件,meta屬性加個(gè)是否可見(visiable true或false)
- home 基本導(dǎo)航欄頁(yè)面邏輯,首先 可以獲得到 所有一級(jí)菜單和角色標(biāo)記
- for 循環(huán)一級(jí)菜單
- 找出角色 所在的 角色數(shù)組(判斷某個(gè)值在不在 數(shù)組中)
- 然后 所在的數(shù)組 visiable 改為true ,其他的改為false
ui框架 是ant design of vue
主要邏輯代碼
1-登錄頁(yè)面

2- 路由頁(yè)面

3- home 菜單欄公用頁(yè)面


全部頁(yè)面代碼
1. 登錄頁(yè)面
<template>
<div class='container'>
<div class="bg-image">
<p @click="demo">登錄</p>
</div>
</div>
</template>
<script>
export default {
name: "",
data() {
return {
role:'user'
};
},
methods: {
demo(){
if (this.role === 'superadmin') {
window.localStorage.setItem('roles','superadmin')
} else if (this.role === 'admin') {
window.localStorage.setItem('roles','admin')
} else if (this.role === 'user') {
window.localStorage.setItem('roles','user')
}
}
},
};
</script>
<style scoped>
</style>
2. 路由js文件
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
Vue.use(Router)
export default new Router({
routes: [
//一級(jí)路由
{
path: "/login",
name: "Login",
meta: { requireAuth: false },
component: require("@/view/Login").default,
},
// 提供頁(yè)面框架
{
path: "/",
name: "Home",
meta: { requireAuth: true },
component: require("@/view/Home").default,
redirect: "/index",
children:[
{
path: '/HelloWorld',
name: 'HelloWorld',
component: HelloWorld,
meta: {requireAuth: false, visiable: true,roles: ['superadmin','admin','user']}
},
{
path: '/SuperAdmin',
name: 'SuperAdmin',
component: () => import("@/view/SuperAdmin.vue"),
meta: {requireAuth: false, visiable: true, roles: ['superadmin']}
},
{
path: '/Admin',
name: 'Admin',
component: () => import("@/view/Admin.vue"),
meta: {requireAuth: false, visiable: true, roles: ['admin']}
},
{
path: '/User',
name: 'User',
component: () => import("@/view/User.vue"),
meta: {requireAuth: false, visiable: true,roles: ['user']}
},
],
}
]
})
3. home公用菜單欄頁(yè)面(ui框架 是ant design of vue)
<template>
<a-layout id="components-layout-demo-custom-trigger">
<a-layout-sider v-model="collapsed" :trigger="null" collapsible>
<div class="logo">
<span class="anticon">
<!-- <img src="../assets/image/logo.png" alt="" /> -->
</span>
<p v-if="!collapsed">項(xiàng)目名</p>
</div>
<a-menu
theme="dark"
mode="inline"
:defaultSelectedKeys="[current]"
:selectedKeys="[current]"
@click="handleClick"
@openChange="onOpenChange"
:open-keys="openKeys"
>
<template v-for="item in menuList">
<!-- 有一級(jí)以上 -->
<a-sub-menu
:key="item.path"
v-if="item.children != undefined && item.meta.visiable"
>
<p slot="title">
<span class="anticon"> <a-icon :type="item.meta.icon" /> </span>
<span>{{ item.name }} </span>
</p>
<!-- 二級(jí)頁(yè)面 -->
<template v-for="child in item.children">
<a-menu-item :key="child.path" v-if="child.meta.visiable">
<p class="ciclebox">
<span> {{ child.name }}</span>
</p>
</a-menu-item>
</template>
</a-sub-menu>
<!-- 普通只有一級(jí)的頁(yè)面 -->
<a-menu-item
:key="item.path"
v-if="item.children == undefined && item.meta.visiable"
>
<a-icon :type="item.meta.icon" />
<span>{{ item.name }}</span>
</a-menu-item>
</template>
</a-menu>
</a-layout-sider>
<a-layout :style="'width:' + (fullWidth - 200) + 'px'">
<a-layout-header style="background: #fff; padding: 0; height: 60px">
<div class="headerflex">
<a-icon
class="trigger"
:type="collapsed ? 'menu-unfold' : 'menu-fold'"
@click="() => (collapsed = !collapsed)"
/>
<div class="app-header-right">
<a-menu mode="horizontal" @click="handleClickLogin">
<a-sub-menu>
<span class="app-user-avatar" slot="title">
<a-avatar
size="small"
src="https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png"
/>
<!-- 用戶名 -->
<!-- {{ User ? User.username : "" }} -->
<a-icon type="down" class="icon" />
</span>
<!-- <a-menu-item key="/setting/password"
><a-icon type="lock" key="password" />修改密碼
</a-menu-item> -->
<a-menu-item key="/login"
><a-icon type="poweroff" />退出登錄
</a-menu-item>
</a-sub-menu>
</a-menu>
</div>
</div>
</a-layout-header>
<a-layout-content style="padding-top: 4px">
<!-- 這里顯示頁(yè)面內(nèi)容 -->
<router-view></router-view>
</a-layout-content>
</a-layout>
</a-layout>
</template>
<script>
import routes from "../router";
export default {
name: "Home",
components: {},
data() {
return {
collapsed: false,
menuList: [], //菜單循環(huán)列表
subList: [], // 二級(jí)子菜單
type: "", // 請(qǐng)求參數(shù)type類型
typeshow: false, // false 沒提醒 true 有提醒
current: this.$route.path,
openKeys: [],
rootSubmenuKeys: [],
flag: false,
fullWidth: document.documentElement.clientWidth,
// 消息提醒
num: "", // 回收員審核
ordernum: "", // 訂單列表
cycleordernum: "", // 周期回收列表
promoterverify: "", // 推廣審核
userwithdraw: "", // 會(huì)員提現(xiàn)審核
recyclewithdraw: "", // 回收員提現(xiàn)審核
promotionwithdraw: "", // 推廣員提現(xiàn)審核
roles:'', // 角色顯示
};
},
created() {
let that = this;
this.menuList = routes.options.routes[1].children;
console.log(this.menuList, "一級(jí)菜單");
this.rootSubmenuKeys = this.menuList;
this.roles = window.localStorage.getItem('roles');
this.menuList.forEach(element => {
// 1- for 循環(huán)一級(jí)菜單
// 2-找出角色 所在的 角色數(shù)組(判斷某個(gè)值在不在 數(shù)組中)
// 3- 然后 所在的數(shù)組 visiable 改為true ,其他的改為false
element.meta.roles.forEach(item => {
if( item.includes(that.roles)){
// 存在
element.meta.visiable = true
} else {
// 不存在
element.meta.visiable = false
}
});
});
},
mounted() {
//監(jiān)聽屏幕寬度
const that = this;
window.onresize = () => {
return (() => {
window.fullWidth = document.documentElement.clientWidth;
that.fullWidth = window.fullWidth;
})();
};
},
methods: {
onOpenChange(openKeys) {
// 最新的key值
const latestOpenKey = openKeys.find(
(key) => this.openKeys.indexOf(key) === -1
);
if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
this.openKeys = openKeys;
} else {
this.openKeys = latestOpenKey ? [latestOpenKey] : [];
}
},
handleClick(e) {
//如果key為路由則跳轉(zhuǎn)
this.current = e.key;
if (e.key.indexOf("/") > -1) {
this.$router.push(e.key).catch(() => {});
}
},
// 退出登錄
handleClickLogin(e) {
this.current = e.key;
this.openCurrent = e.key;
if (e.key == "/login") {
localStorage.clear();
setTimeout(() => {
this.$router.replace({ path: "/logout", name: "Login" });
}, 300);
}
},
},
};
</script>
<style>
/* 退出登錄 */
.headerflex {
display: flex;
align-items: center;
justify-content: space-between;
}
.rights {
padding: 0 24px;
}
#components-layout-demo-custom-trigger .trigger {
font-size: 18px;
height: 60px !important;
line-height: 60px !important;
padding: 0 24px;
cursor: pointer;
transition: color 0.3s;
}
#components-layout-demo-custom-trigger .trigger:hover {
color: #1890ff;
}
#components-layout-demo-custom-trigger .logo {
height: 52px;
margin: 16px 16px 10px;
display: flex;
align-items: center;
}
#components-layout-demo-custom-trigger .logo img {
width: 30px;
height: 30px;
margin-right: 10px;
}
#components-layout-demo-custom-trigger .logo p {
color: #fff;
margin-bottom: 0;
font-size: 16px;
font-weight: 700;
}
.ciclebox {
position: relative;
}
.ciclered {
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
display: inline-block;
width: 15px;
height: 15px;
border-radius: 50%;
text-align: center;
line-height: 15px;
background-color: #e72c0b;
color: #fff;
font-size: 12px;
}
.icon {
padding-left: 10px;
}
.app-header-right .ant-menu-horizontal {
line-height: 60px !important;
}
.app-user-avatar img{
width:20px;
height:20px;
}
* p {
margin-bottom: 0 !important;
}
* ul,
* li {
padding: 0;
margin: 0;
list-style: none;
}
.ant-card-body {
padding-top: 20px;
}
.change-inline {
display: inline-block;
}
.container .page-header {
height: 84px;
padding: 16px;
background-color: #fff;
}
.container .page-header h3 {
font-size: 20px;
padding-top: 8px;
font-weight: 600;
}
.container .claear-top-height {
height: auto !important;
}
.container .infomation-box-top {
margin-top: 10px;
}
.container .infomation-box-top h3 {
font-weight: 600;
margin-bottom: 14px;
}
.container .infomation-box-top p {
margin-bottom: 0;
line-height: 38px;
color: #4b4b4b;
}
.container .infomation-box-top .status-identity {
height: 108px;
display: flex;
align-items: flex-end;
justify-content: center;
}
.container .infomation-box-top .status-identity .type-title {
color: #666666;
font-weight: 600;
}
.container .infomation-box-top .status-identity p {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.container .infomation-box-top .status-identity .set-width {
width: 80%;
}
.container .infomation-box-top .status-identity .set-width .add-right-border {
height: 76px;
border-right: 2px solid #F4F4F4;
}
.container .infomation-box-top .status-identity .set-width .add-right-border span {
font-weight: 600;
}
.container .infomation-box-top .status-identity .set-width .add-right-border .money-edit {
display: flex;
align-items: center;
justify-content: space-between;
}
.container .infomation-box-top .status-identity .set-width .add-right-border .anticon {
margin-right: 10% !important;
}
.container .infomation-box-top .status-identity .set-width .add-right-border .edit-icon-color {
color: #2B85E4;
}
.container .set-flex-end {
display: flex;
justify-content: flex-end;
margin-bottom: 16px;
}
.container .set-flex-end .button-group {
display: flex;
align-items: center;
}
.container .set-flex-end .button-group .search {
margin-right: 16px;
}
.container .table-style .recommender {
text-decoration: underline;
color: #2B85E4;
}
.container .table-style .table-role {
margin-bottom: 0;
cursor: default;
}
.container .table-style .role-r {
color: #19BE6B;
}
.container .table-style .role-s {
color: #FF9900;
}
.container .table-style .role-t {
color: #2B85E4;
}
.container .table-style .role-w {
color: #F56C6C;
}
.container .table-style .role-c {
color: #8b8b8b;
}
.container .table-style .text-detail {
color: #409EFF;
padding: auto 10px !important;
}
.container .table-style .addleft-padding {
padding-left: 10px;
}
.container .table-style .order-to {
color: #409EFF;
padding-left: 10px !important;
}
.container .table-style .text-stop {
color: #F56C6C;
padding-left: 10px !important;
}
.container .table-style .text-stopis {
color: #2fc25b;
padding-left: 10px !important;
}
.container .pagination-margin {
margin-top: 25px;
display: flex;
justify-content: flex-end;
}
.container .ant-tabs-bar {
padding-left: 30px;
background: #fff;
font-weight: 600;
margin: 0 0 26px !important;
}
.container .ant-tabs-nav {
font-size: 15px !important;
}
.container .ant-tabs-nav .ant-tabs-tab {
margin-right: 0 !important;
padding-bottom: 20px;
}
.container .tab-pane-set {
padding: 0 2%;
}
.container .ant-card-head-title {
padding: 0px;
min-height: 44px !important;
line-height: 44px;
font-size: 15px;
font-weight: 600;
color: #6b6b6b;
}
.padbox {
margin-left: 10px;
}
.appoint {
background: #e72c0b;
color: #e72c0b !important;
}
.wait {
background: #ff9900;
color: #ff9900 !important;
}
.pass {
background: #2fc25b;
color: #2fc25b;
}
.success {
background: #409eff;
color: #409eff !important;
}
.cancel {
background: #8b8b8b;
color: #8b8b8b;
}
.red {
color: #e72c0b !important;
}
.appoint_cl {
color: #e72c0b !important;
}
.wait_cl {
color: #ff9900 !important;
}
.pass_cl {
color: #2fc25b !important;
}
.success_cl {
color: #409eff !important;
}
.cancel_cl {
color: #8b8b8b !important;
}
.clear-bg {
background: none !important;
}
</style>
項(xiàng)目目錄

到此這篇關(guān)于vue router權(quán)限管理實(shí)現(xiàn)不同角色顯示不同路由的文章就介紹到這了,更多相關(guān)vue router 不同角色顯示不同路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue中實(shí)現(xiàn)權(quán)限管理詳解
- vue登錄路由權(quán)限管理的項(xiàng)目實(shí)踐
- vue實(shí)現(xiàn)前端按鈕組件權(quán)限管理
- Vue?element實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)流程詳解
- 深入解析vue中的權(quán)限管理
- 淺談vue權(quán)限管理實(shí)現(xiàn)及流程
- Vue 指令實(shí)現(xiàn)按鈕級(jí)別權(quán)限管理功能
- vue權(quán)限管理系統(tǒng)的實(shí)現(xiàn)代碼
- 關(guān)于Vue的路由權(quán)限管理的示例代碼
- vue 權(quán)限管理幾種實(shí)現(xiàn)方法
相關(guān)文章
vue-router 手勢(shì)滑動(dòng)觸發(fā)返回功能
這篇文章主要介紹了vue-router 手勢(shì)滑動(dòng)觸發(fā)返回功能,文中通過實(shí)例代碼給大家介紹了vue圖片左右滑動(dòng)及手勢(shì)縮放,需要的朋友可以參考下2018-09-09
Vue中router.beforeEach與beforeRouteEnter的區(qū)別及說明
這篇文章主要介紹了Vue中router.beforeEach與beforeRouteEnter的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue實(shí)現(xiàn)動(dòng)態(tài)表格提交參數(shù)動(dòng)態(tài)生成控件的操作
這篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)表格提交參數(shù)動(dòng)態(tài)生成控件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue表單綁定實(shí)現(xiàn)多選框和下拉列表的實(shí)例
本篇文章主要介紹了vue表單綁定實(shí)現(xiàn)多選框和下拉列表的實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Vue3.2?新增指令?v-memo?用法詳解(提高性能利器)
v-memo 接受一個(gè)依賴的數(shù)組,依賴的數(shù)組變化,v-memo 所對(duì)應(yīng)的 DOM 包括子集將會(huì)重新渲染,這篇文章主要介紹了Vue3.2?新增指令?v-memo?用法,提高性能的又一利器,需要的朋友可以參考下2022-09-09
Vue實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)el-select組件的示例代碼
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)el-select組件的方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的的可以參考一下2023-02-02

