Preface: When it comes to state management, you may immediately think of: 1. What is front-end state management?For example: everyone in the library can freely enter the library to borrow and return books. If there are not many people, this method can improve efficiency and reduce the number of processes. However, once there are many people, it will easily cause confusion, the whereabouts of the books will be unclear, or even lost. Therefore, you need a librarian to specifically record the borrowing of books, that is, you have to entrust the librarian to borrow and return books for you. In fact, most state management solutions are based on the above idea, using administrators (such as Vuex) to regulate the borrowing and returning of books in the library (data that needs to be stored in the project) 2. Vuex The proportion of const state = { book: 0 } const mutations = { borrow_book(state) { state.book++ } } //When calling store.commit('borrow_book') What about In fact, I just use Vuex to briefly introduce the related usage. Everyone should be familiar with it. So what problem does Vuex solve?
In fact, most programmers are lazy (for the life of them), and they only want to share state among multiple components, and everything else is afterthought. The most typical example is the number of items added to the shopping cart. When one item is added, the final total is saved through Vuex records and displayed in the lower column. Now the question is, since your purpose is just to share multiple states, why not just use 3. Bus In fact, the Bus is very lightweight. It does not have a Vue.prototype.$Bus = new Vue() Then, you can send events via //Send event this.$Bus.$emit('borrow_book', 1) // Received in any component this.$Bus.$on('borrow_book', (book) => { console.log(`Borrowed ${book} book`) }) Of course, there are also operations such as How about it? Is the above much simpler than Vuex for sharing a state? In fact, it is much simpler, but this also means that it is more suitable for small and medium-sized projects. For large projects, Its working principle is the idea of publishing and subscribing. Although it is very elegant and simple, class Bus { constructor() { // Collect subscription information, dispatch center this.list = {}; } // Subscribe$on(name, fn) { this.list[name] = this.list[name] || []; this.list[name].push(fn); } // Publish $emit(name, data) { if (this.list[name]) { this.list[name].forEach((fn) => { fn(data); }); } } // Unsubscribe $off(name) { if (this.list[name]) { delete this.list[name]; } } } export default Bus; Simple, right? You just need to instantiate it and use it just like you would with 4. Web storage In fact, when it comes to this, There are three types No matter which of these three, it is strongly recommended not to put sensitive information in it. It should be encrypted or contain some less important data. Let’s briefly review the three: There is no need to say much about cookie . When people make requests, they often carry cokie to request some personal data, etc., which is not very relevant to what we are going to discuss.
The only difference between Summarize: No matter which solution you choose, it is the best practice to choose the one that suits your project. There is no best solution, only the solution that suits you. This is the end of this article about front-end status management. For more content related to front-end status management, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Limiting the number of short-term accesses to a certain IP based on Nginx
>>: HTML table tag tutorial (17): table title vertical alignment attribute VALIGN
Table of contents Overview 1. Separation of front...
Detailed explanation of the misplacement of the in...
All blogs listed below are original and uniquely ...
Step 1: Check the local Ethernet properties to se...
For what I am going to write today, the program r...
MySQL 5.0 has become a classic because of its few...
As a programmer who has just learned Tomcat, this...
This article records the installation and configu...
1. Introduction When writing animation effects fo...
First, let me give you an example (if you don’t w...
Methods for changing passwords before MySQL 5.7: ...
What is vuex vuex: is a state manager developed s...
When the page is not responding, displaying the l...
Table of contents Introduction: Installation of e...
Preface This article only focuses on what Nginx c...