current position:Home>Using vuex in Vue projects
Using vuex in Vue projects
2022-04-29 16:10:31【X Beichen North】
explain
Here is only a simple record , I didn't sort it out in detail .
step
1、 Install the project in the root directory through the command vuex
npm install vuex --save-dev
2、 And then in src Create a new one in the directory store Folder , Create a new... In the folder index.js file , Enter the following code in this file :
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store();
export default store;
3、 And then in main.js In the document , take store Global injection , In this way, you can use... In each component this.$store 了 , as follows :
import Vue from 'vue'
import App from './components/system/index/Index.vue';
import store from './store';
Vue.config.productionTip = false
new Vue({
store,
render: h => h(App),
}).$mount('#app')
4、 Return to our new store In the catalog index.js file , add to state Object and a test property , And then rewrite Store() Method instantiation code , as follows :
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const state = {
_defaultThemeColor: '#409EFF', // The default theme background color
};
const store = new Vuex.Store({
state
});
export default store;
5、 After completing the above steps , We can use... In other components this.$store.state._defaultThemeColor To get us in index.js The attribute value defined in .
6、 But the way to get the value above is not the way we officially recommend , Officials provided one getters To get the value , The code is as follows :
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const state = {
_defaultThemeColor: '#409EFF', // The default theme background color
};
const getters = {
_getDefaultThemeColor() { // Name at will
return state._defaultThemeColor;
}
};
const store = new Vuex.Store({
state,
getters
});
export default store;
7、 In addition to the value , We also deal with assignment , So we can pass mutations To modify our store The values defined inside , as follows :
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const state = {
_defaultThemeColor: '#409EFF', // The default theme background color
};
const getters = {
_getDefaultThemeColor() { // Name at will
return state._defaultThemeColor;
}
};
const mutations = {
_setDefaultThemeColor(state,value) {
state._defaultThemeColor = value;
}
};
const store = new Vuex.Store({
state,
getters,
mutations
});
export default store;
8、 It's time to go through this.$store.commit('newNum',6) To change store It's worth .
copyright notice
author[X Beichen North],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291427264271.html
The sidebar is recommended
- Rasa dialogue robot serial 7 lesson 122: rasa dialogue robot debugging project practical bank financial dialogue robot whole life cycle debugging practice - (III)
- CSS foundation-15-drop-down menu
- Only one message prompt pops up in the element UI at a time
- Leetcode 82. Delete duplicate elements in the sorting linked list II
- This beast was blessed with skills to test drive the DHT version of Harvard beast
- Vue Click to switch the background color randomly (small demo)
- In the era of man-machine war, how did Beijing magic cube and artificial intelligence produce chemical reaction
- About nginx + Nacos using domain name connection invalid things
- How strong is the giant wave hybrid blessing when GAC motor shadow cool makes its global debut?
- Layui framework application FAQ
guess what you like
Layui style optimization
Post request (http-c4e7.post)
Is low code a blessing or a challenge for programmers?
Use the pointer of the array to test the relationship between the two-dimensional elements and the column of the array in the order of "% 1-6", and then use the pointer of the array to output the data in the order of "% 1-6", and then use the pointer of t
6-2 pointer and the sum fraction of each column of array matrix 10 this problem requires the implementation of a function to find the sum of each column of a two-dimensional array with n (less than 10) rows and 7 columns. The columns and are stored in a o
7-1 find the specified element in the array
When using uniapp for e-commerce projects, vuex is used to realize the global sharing of data to make shopping cart pages
JQuery Basics
`JQuery ` advanced
Do not leave the market unless necessary! Zhongshan City issued specific requirements for campus epidemic prevention after the May Day holiday
Random recommended
- Software design pattern -- Chapter 3 structural pattern -- sharing element pattern
- Vue uses the online form of Excel in the front end of lucky sheet to import, display and export Excel files
- Vue uses echart to draw national maps and overlay charts
- Vue + element UI: Vue user-defined instruction monitors the scrolling event of El table to scroll the scroll bar to the bottom and load new data
- Vue + element when there is no paging at the back end, the front end completes the paging of El table independently - scrolling to the bottom to load new data
- [react] react routing concept
- Lenovo z475 disassembly and repair - plate No. kl6c
- Random array into an array, requiring that the elements cannot be repeated
- The belated Toyota bz4x, even with the blessing of e-tnga architecture, is still not worth starting
- In element plus, for example, how to change the checkbox status in the list by clicking on the header and selecting all
- Crawler reverse advanced, using ast technology to restore JavaScript obfuscated code
- Help, after changing the user name, the computer is useless and can't log in
- Drag the left column of Vue and keep the right width unchanged; The scroll bar appears
- HTML notes
- In depth analysis of headless single linked list -- dynamic diagram demonstration of C language
- Share 9 development skills related to vue3
- CSS box centered
- Used in Vue projects Sync modifier and $emit (update: XXX)
- Vue class & Style binding and computed
- Vue project uses this$ forceUpdate(); Force render page
- HTTP becomes HTTPS, self-made certificate
- Web front-end operation - tourism enterprise marketing publicity responsive website template (HTML + CSS + JavaScript)
- Self inspection list of a [qualified] front-end Engineer
- This principle in JavaScript and six common usage scenarios
- JavaScript this priority
- Analyzing the principle of HTTPS encryption
- Difference and principle between websocket and http
- Use of elementui scroll bar component El scrollbar
- Nginx security optimization
- GAC group has become the first pilot enterprise of "yueyouhang". Blessed are the car owners in Guangdong!
- Loki HTTP API usage
- JavaScript - prototype, prototype chain
- Front end experience
- JavaScript -- Inheritance
- HTTP cache
- Filters usage of table in elementui
- A JavaScript pit encountered by a non front-end siege lion
- Grain College - image error when writing Vue with vscode
- Utility gadget - get the IP address in the HTTP request
- Could not fetch URL https://pypi.org/simple/pytest-html/: There was a problem confirming the ssl cer