current position:Home>Summary of Vue basics 14: what does vuex do?
Summary of Vue basics 14: what does vuex do?
2022-05-15 03:08:38【nezha】
Catalog
One 、 Official explanation
Vuex Is a special for Vue.js State management mode of application development .
It uses Centralized storage management The state of all components of the application , And the corresponding rules ensure that the state changes in a predictable way .
Two 、Vuex Legend of state management
3、 ... and 、VueX Core concept of
- State
- Getters
- Mutation
- Action
- Module
Four 、getters
from store Get some state The state of being mutated .
export default {
powerCounter(state) {
return state.counter * state.counter
},
more20stu(state) {
return state.students.filter(s => s.age > 20)
},
more20stuLength(state, getters) {
return getters.more20stu.length
},
moreAgeStu(state) {
return age => {
return state.students.filter(s => s.age > age)
}
}
}
5、 ... and 、Mutation
Vuex Of store The only way to update the status : Submit Mutation
Mutation It mainly consists of two parts :
- The event type of the string ;
- A callback function (handler), The first parameter of the callback function is state;
Mutation How to define :
mutations: {
increment(state) {
state.count++
}
},
adopt Mutation to update :
increment:function() {
this.$store.commit('increment')
}
Through Mutation When updating data , Possible parameters , The parameter is called Mutation The load of (Payload).
increment(state, x){
state.count += x;
}
increment: function(){
this.$store.commit('increment', x)
}
What if it's multiple objects ?
Then it can be passed as an object .
6、 ... and 、Action
Instead of Mutation Do asynchronous operations .
actions: {
increment(context) {
setTimeout(() => {
context.commit('increment')
}, 1000)
}
}
context What is it? ?
context Is and store Objects that have the same methods and properties , That is to say, it can pass through context Conduct commit, Also available context.state.
stay Vue In the component , If we call action The method in , Then we need to use dispatch Method .
actions: {
increment(context) {
setTimeout(() => {
this.$store.dispatch('increment')
}, 1000)
}
}
action Back to Promise.
stay Action in , We can put asynchronous operations in one Promise in , And after success or failure , Call corresponding resolve or reject.
actions: {
increment(context) {
return new Promise((resolve => {
setTimeout(() => {
context.commit('increment')
}, 1000)
}))
}
}
methods: {
increment() {
this.$store.dispatch('increment').then(res => {
console.log(' Official account 【 Nezha programming 】, obtain java Development of information ')
})
}
}
7、 ... and 、Module
Vue take store To divide into several module, Each module has its own state、mutations、actions、getters etc. .
Last one :Vue Basic knowledge summary 13: Build from scratch Vue project
Next :Vue Why abandon the classic Ajax, Choose new technology Axios?
copyright notice
author[nezha],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/132/202205112349422271.html
The sidebar is recommended
- Build an electron project based on Vue from scratch
- In Vue project, solve the problem of verification conflict when eslint and prettier are used at the same time
- Vue3 + vuecli4 solve chunk vendors JS file is too large. Scheme summary
- Scheme summary of eslint check before vue3 + vite configuration project operation and eslint check before git submission
- Fatal authentication failed for 'httpxxxx Git 'solution
- Vue's solution to the compatibility of hevc encoded video in each end browser
- Record the solution to the error in obtaining the picture in springboot in Vue
- [Vue] detailed analysis of the life cycle function of Vue components
- [Vue] deeper understanding of user-defined attribute props
- [Vue] front end error: cannot read properties of undefined (reading 'split')
guess what you like
[Vue] asynchronous problem of component value transfer -- the sub component gets the data slowly
[Vue] Vue data changes, but the page is not updated in real time
[element UI] use of form verification -- detailed explanation
[Vue] use of slots - Review
The influence on the fixed positioning element of the inner layer when the outer element has a fixed height and overflows and rolls
Precautions
Change detection strategy of angular component
Angular service and @ injectable
JS, HTML and CSS are not compatible and do not use common knowledge
Properties to be known in angular @ component
Random recommended
- Angular acquisition and operation DOM
- Html2canvas problem
- Use day in Vue JS (time and date processing library)
- Vue cli configuring preprocessor global variables (take less as an example)
- How to use H5 custom tags in Vue?
- Come on, vue2 customize global loading
- Come on, Vue move the end suspension ball assembly
- React routing foundation and transmission parameters
- Come on, Vue graphic verification code component
- JavaScript judges browser types and kernels at home and abroad (including 360, QQ, Sogou, etc.)
- ArcGIS JavaScript 4. Generates a rectangular buffer at the point of X
- Node under window JS installation, environment configuration, setting Taobao image
- Understanding of prototype and prototype object of JavaScript
- How to customize the startup port of react project!
- Why vue3 0 using proxy to realize data listening 2021-06-21
- Front end artifact - download, configuration and use process of Charles (vase) [Mac version]
- React next configures SVG loader and supports SVG configurability
- React native Android phone cannot be opened after installation. Flash back!
- Fetch and Axios failed to request on Android, with error messages: network request failed and network error
- How to upgrade react Babel 7.1.0
- babel7. 0 compatible with IE browser
- Nginx configuring reactrouter browserhistory browserrouter
- JS, react use html2canvas page screenshot and export
- Big data front-end visualization screen -- the road of front-end development
- [a quick introduction and comparison of multiple languages -- JavaScript, typescript, python, golang, trust, Java, ruby]
- Vue element admin login function, token filling, language conversion, routing setting
- Summation of corresponding position elements of multiple lists in Python
- Discussion on HTML page knowledge
- Using Ajax to realize non refresh paging
- HTTP format
- Zhang San has a meal - did he eat the difference between get and post in HTTP?
- The ultimate actual combat of the most complete tourism project based on spring boot + Vue front-end and back-end separation in history (with source code attached), none of them!!!
- Vue basic grammar
- LeetCode 217. There are duplicate elements
- Pagoda does not configure the solution of SSL site accessing HTTPS and jumping to other websites
- Vue3 isProxy
- About the problem that the container will hang up after starting nginx in the docker container
- Introduction to JavaScript
- The core element of large-scale software design is to control complexity. What is the root cause?
- What are the core elements of large-scale software design?