Vuex combines localstorage to dynamically monitor storage changes
This article mainly introduces the detailed explanation of vuex combined with localstorage to dynamically monitor storage changes. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
Requirements: Different components share the same data. When one component changes the data, other components can also respond to the change.
Analysis: vue cannot monitor localstorage changes. Localstorage is mainly used to transfer values between different pages, while vue is suitable for transferring values between components. For components that share the same data and want to save the information or not lose the data when the page is refreshed (the value stored by vuex will be lost when the page is refreshed, and localstorage is stored in the local browser), you can use vuex localstorage.
About the difference between vuex and storage
1. The most important difference: vuex is stored in memory, while localstorage is stored locally in the form of files
2. Application scenarios: vuex is used to transfer values between components, while localstorage is mainly used to transfer values between different pages.
3. Permanence: The value stored by vuex will be lost when the page is refreshed, but localstorage will not.
Note: Many students think that localstorage can be used instead of vuex. It is indeed possible for unchanged data, but when two components share a data source (object or array), if one of the components changes the data source , when you want another component to respond to the change, localstorage cannot do it, the reason is difference 1.
Implementation process: Take the home page to display user avatar information and modify personal information in the public component header component as an example. When the user modifies personal information, the picture on the home page changes in real time. If the avatar information is not stored and updated, every After the user has made the modification, he or she can see the changes only by refreshing the page or returning from other pages, that is, the newly set avatar information, and only the core code is displayed.
1. First define a variable in state. State is responsible for storing the state data of the entire application. You can use this.$store.state to directly obtain the state later. You can also use the mapState auxiliary function provided by vuex to map state to calculated properties.
const state = { imgInfo:null //首页头像信息 }
2.mutations store localstorage information. Mutations can change the state, which is essentially a function used to process data, which receives the unique parameter value state. The defined mutation must be a synchronous function. this.$store.commit(mutationName) is a method used to trigger a mutation, or use the auxiliary function mapMutations to directly map the trigger function to methods, so that it can be used directly in element event binding.
export const SETIMGINFO = 'SETIMGINFO' [SETIMGINFO] (state,info) { state.imgInfo=info localStorage.setItem('imgInfo',info) }
3. Get the localstorage information in the getter. Some states require secondary processing, so you can use getters. Access the derived state through this.$store.getters.valueName. Or directly use the auxiliary function mapGetters to map it to local calculated properties.
getImgInfo(state){ if(localStorage.getItem('imgInfo')){ state.imgInfo=localStorage.getItem('imgInfo') } return state.imgInfo }
4. Reference the mapMutations function on the page that needs to operate on storage
import {mapMutations} from 'vuex' //引入mapMutations ...mapMutations([ 'SETIMGINFO' ]), this.SETIMGINFO(this.imgInfo) //在需要的地方引用 mutations里面定义的方法
5. Reference the mapGetters auxiliary function on the page where storage information needs to be obtained
import {mapGetters} from 'vuex' computed:{ ...mapGetters([ 'getImgInfo' ]) }, watch:{ //动态监听state的变化,实时改变页面的数据 getImgInfo: function(li) { //li就是改变后的state里面的imgInfo let vm = this; this.imgInfo=li //data声明一个变量,在html引用。如果storage的值发生变化就实时刷新变量的值。 } },
6. Reference to the vuex value in the template
<img :src="imgInfo?imgInfo:info.avatar"> //三元不等式,如果state发生变化有值就赋值给img标签,如果没有即刚进页面就赋值给create生命周期函数中从接口读出来的数据
Related recommendations:
How to implement query dynamic parameter passing in vue-router
The above is the detailed content of Vuex combines localstorage to dynamically monitor storage changes. 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

Why does storing data to localstorage always fail? Need specific code examples In front-end development, we often need to store data on the browser side to improve user experience and facilitate subsequent data access. Localstorage is a technology provided by HTML5 for client-side data storage. It provides a simple way to store data and maintain data persistence after the page is refreshed or closed. However, when we use localstorage for data storage, sometimes

How to set the expiration time of localstorage requires specific code examples. With the rapid development of the Internet, front-end development often requires saving data in the browser. Localstorage is a commonly used WebAPI that aims to provide a way to store data locally in the browser. However, localstorage does not provide a direct way to set the expiration time. This article will introduce how to set the expiration time of localstorage through code examples.

How to monitor the scrolling of an iframe requires specific code examples. When we use the iframe tag to embed other web pages in a web page, sometimes we need to perform some specific operations on the content in the iframe. One of the common needs is to listen for the scroll event of the iframe so that the corresponding code can be executed when the scroll occurs. The following will introduce how to use JavaScript to monitor the scrolling of an iframe, and provide specific code examples for reference. Get the iframe element First, we need

How to recover deleted Localstorage data? Localstorage is a technology used to store data in web pages. It is widely used in various web applications to share data between multiple pages. However, sometimes we may accidentally delete data in Localstorage, which causes us trouble. So, how to recover deleted Localstorage data? Below are specific steps and code examples. Step 1: Stop writing to Loca

The reasons why localstorage is unsafe are unencrypted data, XSS attacks, CERF attacks, capacity limitations, etc. Detailed introduction: 1. Data is not encrypted. Localstorage is a simple key-value pair storage system. It stores data in the user's browser in clear text, which means that anyone can easily access and read the data stored in localstorage. If sensitive information is stored in localstorage, hackers or malicious users can easily obtain this information and so on.

Steps and precautions for using localStorage to store data This article mainly introduces how to use localStorage to store data and provides relevant code examples. LocalStorage is a way of storing data in the browser that keeps the data local to the user's computer without going through a server. The following are the steps and things to pay attention to when using localStorage to store data. Step 1: Check whether the browser supports LocalStorage

Why can't localstorage save my data normally? In web development, we often need to save the user's data locally so that the data can be quickly loaded or restored the next time the user visits the website. In the browser, we can use localStorage to achieve this function. However, sometimes we find that data saved using localStorage does not work properly. So why does this happen? In understanding why localStorage
![How to solve the problem 'Error: [vuex] unknown action type: xxx' when using vuex in a Vue application?](https://img.php.cn/upload/article/000/887/227/168766615217161.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
In Vue.js projects, vuex is a very useful state management tool. It helps us share state among multiple components and provides a reliable way to manage state changes. But when using vuex, sometimes you will encounter the error "Error:[vuex]unknownactiontype:xxx". This article will explain the cause and solution of this error. 1. Cause of the error When using vuex, we need to define some actions and mu
