Routing permission control is often used in background management systems to restrict the permissions of pages that different business personnel can access. For pages without permission, you can jump to the 404 page or prompt that you do not have permission. Method 1: Routing meta information (meta) Put all pages in the routing table and just check the role permissions when accessing them. In the meta attribute, add the roles that can access this route to roles. After each user logs in, the user's role is returned. Then when accessing the page, the meta attribute of the route is compared with the user's role. If the user's role is in the roles of the route, then access is allowed. If not, access is denied. Example 1: Judging by roles const myRouter = new VueRouter({ routes: [{ path: '/login', name: 'login', meta: { roles: ['admin', 'user'] }, component: () => import('@/components/Login') },{ path: '/home', name: 'home', meta: { roles: ['admin'] }, component: () => import('@/views/Home') },{ path: '/404', component: () => import('@/components/404') }] }) //Assume that the user role obtained from the background through the interface can be stored in the token const role = 'user' myRouter.beforeEach((to,from,next)=>{ if (to.meta.roles.includes(role)) { next() //release }else{ next({path:"/404"}) //jump to 404 page} }) Example 2: Add an identifier to the meta that requires permissions const myRouter = new VueRouter({ routes: [{ path: '/login', name: 'login', meta: { title: 'Login Page' icon: 'login' }, component: () => import('@/components/Login') },{ path: '/home', name: 'home', meta: { Title: 'Home' icon: 'home', requireAuth: true }, component: () => import('@/views/Home') },{ path: '/404', component: () => import('@/components/404') }] }) myRouter.beforeEach((to,from,next)=>{ let flag = to.matched.some(record=>record.meta.requireAuth); //Here we use the matched loop to find the reason why to.meta is not used directly: //When there are sub-routes, first make it clear that according to the normal click logic, the first-level route is first followed by the second-level route, and the judgment of permissions must be the same //to.meta => will directly search the meta of the sub-route. If the first-level route does not add requireAuth:true, the first-level route page should be blocked and cannot enter the second-level route page; if the user directly tampers with the URL address bar, then the second-level page can be entered, and there may be problems with permissions. Then all routes under this permission should be marked //to.matched => matched is a route array, which will collect the attributes of all routes including sub-routes, so through the loop judgment and search method, you only need to add permission identifiers to the first-level route to authorize other sub-routes under it. if(flag){ next() }else{ next({path:"/404"}) } }) Disadvantages: It is a waste of computing resources to verify each route jump. In addition, routes that users do not have access to should not be mounted in theory. Method 2: Dynamically generate routing table (addRoutes) By dynamically adding menus and routing tables based on user permissions or user attributes, user functions can be customized. //store.js // Extract the routes that need to be dynamically registered into vuex const dynamicRoutes = [ { path: '/manage', name: 'Manage', meta: { requireAuth: true }, component: () => import('./views/Manage') }, { path: '/userCenter', name: 'UserCenter', meta: { requireAuth: true }, component: () => import('./views/UserCenter') } ] Add the userRoutes array in vuex to store the user's customized menu. In setUserInfo, the user's routing table is generated according to the menu returned by the backend. //store.js setUserInfo (state, userInfo) { state.userInfo = userInfo state.auth = true // Mark auth as true when obtaining user information. Of course, you can also directly judge userInfo // Generate user routing table state.userRoutes = dynamicRoutes.filter(route => { return userInfo.menus.some(menu => menu.name === route.name) }) router.addRoutes(state.userRoutes) // Register routes} Modify menu rendering // App.vue <div id="nav"> <router-link to="/">Home</router-link> <router-link to="/login">Login</router-link> <template v-for="(menu, index) of $store.state.userInfo.menus"> <router-link :to="{ name: menu.name }" :key="index">{{menu.title}}</router-link> </template> </div> This is the end of this article about Vue's two ways of implementing routing permission control. For more relevant Vue routing permission control content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Docker container custom hosts network access operation
>>: A brief discussion on the synchronization solution between MySQL and redis cache
What I have been learning recently involves knowl...
As an open source software, Apache is one of the ...
Copy code The code is as follows: <!--[if IE]&...
Table of contents 1 A brief introduction to syste...
Table of contents 1. Three modes of binlog 1.Stat...
This example uses jQuery to implement a mouse dra...
1. Wireless Run PowerShell and enter the command:...
Inline format <colgroup>...</colgroup>...
Before talking about the structural markup of web...
Basic network configuration Although Docker can &...
Table of contents Seconds_Behind_Master Original ...
Problem background: When using docker to deploy t...
Environment: VMware VCSA 6.7 (VMware-VCSA-all-6.7...
The effect is very simple, just copy the following...
Table of contents 1. MySQL wildcard fuzzy query (...