search
HomeWeb Front-endJS TutorialHow to load the permission management module (detailed tutorial)

How to load the permission management module (detailed tutorial)

Jun 11, 2018 pm 05:36 PM
vue componentRights management module

This article explains in detail how to dynamically load VUE components in the permission management module. Friends who have needs in this regard can follow along and learn.

In this article we will mainly talk about login and dynamic loading of components.

Save login status

After the user successfully logs in, the current user's login information needs to be saved locally for later use. The specific implementation is as follows:

Successfully log in and save data

After the login operation is successfully executed, the data is submitted to the store through the commit operation. The core code is as follows:

this.postRequest('/login', {
  username: this.loginForm.username,
  password: this.loginForm.password
}).then(resp=> {
  if (resp && resp.status == 200) {
  var data = resp.data;
  _this.$store.commit('login', data.msg);
  var path = _this.$route.query.redirect;
  _this.$router.replace({path: path == '/' || path == undefined ? '/home' : path});
  }
});

store

The core code of store is as follows:

export default new Vuex.Store({
 state: {
  user: {
   name: window.localStorage.getItem('user' || '[]') == null ? '未登录' : JSON.parse(window.localStorage.getItem('user' || '[]')).name,
   userface: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).userface
  }
 },
 mutations: {
  login(state, user){
   state.user = user;
   window.localStorage.setItem('user', JSON.stringify(user));
  },
  logout(state){
   window.localStorage.removeItem('user');
  }
 }
});

In order to reduce trouble, the data after the user successfully logs in will be saved in localStorage (to prevent the user from pressing Data is lost after F5 refresh), it is stored in the form of a string, and then converted to json when retrieved. When the user logs out, clear the data in localStorage.

Dynamic loading of components

In the permission management module, this is the core of the front-end.

Core idea

After the user successfully logs in, before entering the home page, the user sends a request to the server to obtain the current menu information and component information. The server will The role that the current user has and the resources corresponding to the role return a json string with the following format:

[
  {
    "id": 2,
    "path": "/home",
    "component": "Home",
    "name": "员工资料",
    "iconCls": "fa fa-user-circle-o",
    "children": [
      {
        "id": null,
        "path": "/emp/basic",
        "component": "EmpBasic",
        "name": "基本资料",
        "iconCls": null,
        "children": [],
        "meta": {
          "keepAlive": false,
          "requireAuth": true
        }
      },
      {
        "id": null,
        "path": "/emp/adv",
        "component": "EmpAdv",
        "name": "高级资料",
        "iconCls": null,
        "children": [],
        "meta": {
          "keepAlive": false,
          "requireAuth": true
        }
      }
    ],
    "meta": {
      "keepAlive": false,
      "requireAuth": true
    }
  }
]

After the front end gets this string, it does two things: 1. Dynamically add json to the current route; 2. Save the data to the store, and then each page renders the menu based on the data in the store.

The core idea is not difficult. Let’s take a look at the implementation steps.

Data request timing

This is very important.

Some friends may ask why this is so difficult. Can’t I just request it after logging in successfully? Yes, it is possible to request menu resources after successful login. After the request is received, we will save it in the store for next time use. However, there will be another problem. If the user clicks on a certain sub-child after successfully logging in, Page, enter the sub-page, and then press F5 to refresh. At this time, it is GG, because the data in the store is gone after F5 is refreshed, and we only requested the menu resources once when the login is successful. There are two ideas to solve this problem: 1. Don’t save the menu resources to the store, but save them to localStorage, so that the data is still there even after F5 refreshes; 2. Directly in the mounted method of each page, go Load menu resources once.

Since the menu resources are very sensitive, it is best not to save them locally, so I abandoned option 1. However, the workload of option 2 is a bit heavy, so I took a step to simplify it. Just use the navigation guard in routing.

Routing Navigation Guard

My specific implementation is as follows. First, create a routes array in the store, which is an empty array, and then enable the global routing guard, as follows:

router.beforeEach((to, from, next)=> {
  if (to.name == 'Login') {
   next();
   return;
  }
  var name = store.state.user.name;
  if (name == '未登录') {
   if (to.meta.requireAuth || to.name == null) {
    next({path: '/', query: {redirect: to.path}})
   } else {
    next();
   }
  } else {
   initMenu(router, store);
   next();
  }
 }
)

The code here is very short, let me give a simple explanation:

1. If the page you want to go to is the login page, there is nothing to say about this, just go through it.

2. If it is not a login page, I first get the current login status from the store. If not logged in, use the requireAuth attribute of the meta attribute in the routing to determine whether the page I want to go to requires login. If login is required, , then jump back to the login page, and at the same time pass the path of the page you want to go to as a parameter to the login page, so that you can jump to the target page after successful login. If you do not need to log in, go directly (in fact, there is only the Login page in this project No login required); if you are already logged in, initialize the menu first and then jump.

The operation of the initialization menu is as follows:

export const initMenu = (router, store)=> {
 if (store.state.routes.length > 0) {
  return;
 }
 getRequest("/config/sysmenu").then(resp=> {
  if (resp && resp.status == 200) {
   var fmtRoutes = formatRoutes(resp.data);
   router.addRoutes(fmtRoutes);
   store.commit('initMenu', fmtRoutes);
  }
 })
}
export const formatRoutes = (routes)=> {
 let fmRoutes = [];
 routes.forEach(router=> {
  let {
   path,
   component,
   name,
   meta,
   iconCls,
   children
  } = router;
  if (children && children instanceof Array) {
   children = formatRoutes(children);
  }
  let fmRouter = {
   path: path,
   component(resolve){
    if (component.startsWith("Home")) {
     require(['../components/' + component + '.vue'], resolve)
    } else if (component.startsWith("Emp")) {
     require(['../components/emp/' + component + '.vue'], resolve)
    } else if (component.startsWith("Per")) {
     require(['../components/personnel/' + component + '.vue'], resolve)
    } else if (component.startsWith("Sal")) {
     require(['../components/salary/' + component + '.vue'], resolve)
    } else if (component.startsWith("Sta")) {
     require(['../components/statistics/' + component + '.vue'], resolve)
    } else if (component.startsWith("Sys")) {
     require(['../components/system/' + component + '.vue'], resolve)
    }
   },
   name: name,
   iconCls: iconCls,
   meta: meta,
   children: children
  };
  fmRoutes.push(fmRouter);
 })
 return fmRoutes;
}

In the initialization menu, first determine whether the data in the store exists. If it exists, it means that this jump is a normal jump, not the user Press F5 or enter an address directly in the address bar. Otherwise go to the loading menu. After getting the menu, first use the formatRoutes method to convert the json returned by the server into the format required by the router. Here we mainly convert component, because the component returned by the server is a string, but what is needed in the router is a component, so we Just load the required components dynamically in the formatRoutes method. After the data format is successfully prepared, on the one hand, the data is stored in the store, and on the other hand, the addRoutes method in the routing is used to dynamically add it to the routing.

Menu rendering

Finally, in the Home page, get the menu json from the store and render it into a menu. The relevant code can be viewed in Home.vue. No need to go into details.

OK, after this, different users can see different menus after successfully logging in.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to get the value in select in vue.js (detailed tutorial)

In Vue2.0 How to use filters in series?

How to return to the top through the tween method in the vue project

How to use vue to determine that the input content is all spaces?

How to use js to determine that the input box cannot be a space or null value?

The above is the detailed content of How to load the permission management module (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version