How to use localstorage and sessionstorage in Vue
This article mainly introduces the detailed usage and experience of localstorage and sessionstorage in Vue. Friends in need can follow the editor for reference and study.
1. Several problems exposed in the use of the project
Everyone directly uses localstorage['aaa']='This is a sample string' these native Syntax implementation, this coupling degree is too high. If one day we need to change the implementation method, or do some control on the storage size, then there will be a lot of code that needs to be modified.
The project is very big, so everyone has to start the key The name will inevitably be repeated, and this will also cause global pollution
Because the use of localstorage is not standardized, it results in waste and insufficient storage space
2. Solution
Encapsulate the usage of storage and handle it uniformly
Standardize the naming rules of the key value of storage
Standardize the usage of storage
2.1. Encapsulate the unified method
Encapsulating the method can reduce coupling, facilitate switching of implementation methods, and control the storage size
Changing the implementation can be achieved by configuring different parameters
Edit as shown The project structure shown
Code implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
2.2. Standardize the use of namespace
In order to prevent key value pollution, we can use the namespace reasonably
We can define a namespace, but we cannot store a lot of data in the same object, so the amount of subsequent operations will be too large
For example, the global one is under global
For example, the addition of each functional system System affixes
The namespace specification of a system should be designed in advance, otherwise many people will use it without following the rules during actual development
Things used globally must be reflected in the README.md document Come out
Example
1 2 3 4 5 6 7 8 9 |
|
2.3. Storage usage specifications
2.3.1. Cause of the problem
This problem occurs because we need to do permissions After logging in, I kept getting reports of insufficient storage space. After checking the reason, I found that the backend returned all the thousands of super-managed data, which was not enough. Later, I modified the data returned by the backend interface. Content solves this problem.
But what has this incident brought us to think about?
The storage capacity of localstorage and sessionstorage is basically 5M in different browsers
The storage of localstorage and sessionstorage follows the domain name
localstorage under boss.hivescm.com The storage is 5M
The localstorage storage under b2b.hivescm.com is also 5M
Even if the problem is solved this time, we should make a plan to make full use of localstorage and sessionstorage under one domain name. Total 10M space
2.3.2. Storage usage plan
Things used globally, shared, and permanently stored are stored in localstorage
No need for permanent storage Remember to clear things in time after using them
If the amount of data is too large, do not store it locally and change it to dynamic acquisition
You can use Indexeddb with larger storage capacity, but there is compatibility Question
You can limit the number of words to be stored in storage in the implementation plan
Make full and reasonable use of the H5 features of sessionstorage and localstorage
For example: list data is stored in In fact, vuex will also be stored in localstorage
For example: some data for form verification uses sessionstorage
3. Others
3.1. Extension
This can be deduced to the processing of events. Useless events must be cleaned up in time when exiting the vue component.
For example: this.bus.$on('aa') should be used this.bus.$off('aa') uninstall event
3.2. Character length acquisition
1 2 3 4 5 6 7 8 |
|
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
JS plug-in for Web forms (high-quality recommendation)
How to implement online customer service function in jQuery
How to dynamically add to the list in jQuery
About optimization configuration issues in Webpack
In How to build Electron applications in Webpack
Issues about unsafe image paths using Angular4
How to implement the cross coordinates following mouse effect in JS
How to use EasyUI window in jQuery
How to use laydate.js date plug-in in Angular4.0
How to implement label scrolling switching in JS
The above is the detailed content of How to use localstorage and sessionstorage in Vue. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.
