vue 라우팅 권한
router.beforeEch
에서 권한 판단을 합니다.
import Vue from 'vue'
import Router from 'vue-router'
import stores from '../vuex/index'
import user_permissions from "../common/util/user_permissions"
const NoPermission = r => require.ensure([], () => r(require('@/views/NoPermission/NoPermissionPanel.vue')));
const Login = r => require.ensure([], () => r(require('@/views/Login.vue')));
const Home = r => require.ensure([], () => r(require('@/views/layout/Home.vue')));
const OpenApiManager = r => require.ensure([], () => r(require('@/views/openapi/Manager.vue')));
const LogisticsManager = r => require.ensure([], () => r(require('@/views/LogisticsManagement/Manager.vue')));
const LogisticsManager2 = r => require.ensure([], () => r(require('@/views/LogisticsManagement/Manager2.vue')));
const userManager = r => require.ensure([], () => r(require('@/views/userManagement/Manager.vue')));
Vue.use(Router)
export const routes = [
{
path: "/",
hidden: true,
redirect: {
name: "login"
}
},
{
path: "/login",
name: "login",
component: Login,
hidden: true
},
{ path: '*', component: Login },
]
export const asyncRouterMap = [
{
path: "/OpenApi",
name: 'OpenApi',
component: Home,
leaf: true,
meta: {
permissions: ['p_1']
},
children: [
{
path: '/OpenApi/OpenApiManager',
component: OpenApiManager,
name: 'OpenApi',
meta: {
title: "OpenApi",
permissions: ['p_1_1']
}
}
]
},
{
path: "/Logistics",
name: 'Logistics',
component: Home,
leaf: false,
meta: {
permissions: ['p_2']
},
children: [
{
path: '/Logistics/LogisticsManager',
component: LogisticsManager,
name: 'LogisticsManager',
meta: {
title: "LogisticsManager",
permissions: ['p_2_1']
}
},
{
path: '/Logistics/LogisticsManager2',
component: LogisticsManager2,
name: 'LogisticsManager2',
meta: {
title: "LogisticsManager2",
permissions: ['p_2_2']
}
}
]
},
{
path: "/user",
name: ' ',
component: Home,
leaf: true,
meta: {
permissions: ['p_3']
},
children: [
{
path: '/user/userManager',
component: userManager,
name: ' ',
meta: {
title: " ",
permissions: ['p_3_1']
}
}
]
},
{
path: "/NoPermission",
name: 'NoPermission',
component: Home,
leaf: true,
hidden: true,
meta: {
permissions: ['p_no']
},
children: [
{
path: '/NoPermission/page',
component: NoPermission,
name: 'NoPermission',
meta: {
title: "NoPermission",
permissions: ['p_page']
}
}
]
}
]
const router = new Router({
routes
})
router.addRoutes(asyncRouterMap);
router.beforeEach((to, from, next) => {
let jwtToken = sessionStorage.getItem('jwtToken');
stores.state.jwtToken = jwtToken;
if (!stores.state.jwtToken && to.path !== '/login') {//
next({
path: '/login',
});
} else {
stores.dispatch('getPermissionList');
const { permissions } = to.meta;
if (permissions) {
//
const hasPermission = user_permissions.includePermission(permissions)
console.log(hasPermission);
if (!hasPermission) {
next({
path: '/NoPermission/page',
});
return;
}else{
next()
}
}
next()
}
})
export default router
vuex에서 권한 획득 목록 추가 방법 getPermissionList
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
list: [],
loginData: {
PermissionList: []
},
jwtToken:'',
isLogin: false
},
mutations: {
updatePermissionList: (state, payload) => {
state.list = payload
},
getList: (state, payload) => {
state.loginData = payload
}
},
actions: {
LoginDataList: async ({ state, commit }) => {
localStorage.removeItem('loginData');
//
const data = { PermissionList: ['p_1', 'p_1_1', 'p_2','p_2_2', 'p_3', 'p_3_1','p_no', 'p_page'] };
sessionStorage.setItem("jwtToken", 'jwtToken');
localStorage.setItem('loginData', JSON.stringify(data));
commit('updatePermissionList', data.PermissionList);
},
getPermissionList: async ({ state, commit }) => {
//
if (state.list.length) return
const list = localStorage.getItem('loginData') != null ? JSON.parse(localStorage.getItem('loginData')).PermissionList : [];
commit('updatePermissionList', list)
}
},
modules: {
}
});
export default store;
사용 권한 여부를 판단하는 방법 추가: includePermission
import stores from '../../vuex/index';
export default {
/**
* @param permissions
*/
includePermission: function (permissions) {
if (!permissions.length) return true
const permissionList:any = stores.state.list;
return !!permissions.find(permission => permissionList.includes(permission))
}
}
demo 주소:https://github.com/jyfweb/router-permission
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.