vuex中的5個(gè)屬性使用方法舉例講解
一,Vuex簡(jiǎn)介
Vuex是Vue.js的狀態(tài)管理庫(kù),它通過中心化的狀態(tài)管理使得組件間的數(shù)據(jù)共享更加容易。
Vuex包含五個(gè)核心屬性:state、getters、mutations、actions和modules。
Vuex是Vue.js的狀態(tài)管理庫(kù),它提供了一種集中式存儲(chǔ)管理應(yīng)用程序中所有組件的狀態(tài),并將其分離到一個(gè)可預(yù)測(cè)的狀態(tài)容器中。Vuex包括五個(gè)核心屬性:
二,Vuex五個(gè)核心屬性
1:state
state:定義了應(yīng)用程序的狀態(tài),就是我們要管理的數(shù)據(jù)。
存放應(yīng)用程序的狀態(tài)(數(shù)據(jù)),所有組件共享。它是Vue實(shí)例的data屬性的替代品,但是通過它存儲(chǔ)和管理的狀態(tài),可以在整個(gè)應(yīng)用程序中實(shí)現(xiàn)全局共享,即不同的組件可以通過定義的getter和setter訪問同一狀態(tài)數(shù)據(jù)。
const store = new Vuex.Store({
state: {
count: 0
}
})
2:getters
getters:用于獲取State中的狀態(tài),主要用于對(duì)state進(jìn)行邏輯上的組合和應(yīng)用,類似于Vue組件中的計(jì)算屬性。
const store = new Vuex.Store({
state: {
count: 0
},
getters: {
doubleCount(state) {
return state.count * 2
}
}
})
3:mutations
mutations:用于修改state中的數(shù)據(jù),是唯一可以修改state的地方。mutations接收state作為第一個(gè)參數(shù),接收payload作為第二個(gè)參數(shù)。
用于修改State中的狀態(tài),只能同步執(zhí)行。Mutation必須是同步函數(shù),因?yàn)樗鼈儾荒芴幚懋惒叫袨?,異步行為?yīng)該放在Action中處理。
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
},
add(state, payload) {
state.count += payload
}
}
})
4:actions
actions:用于異步操作和提交mutations,在actions中可以進(jìn)行任何異步操作,最后再提交到mutations中同步修改state。actions接收context作為第一個(gè)參數(shù),其中包含了state、getters和commit等屬性。
可以包含任意異步操作(例如從服務(wù)器獲取數(shù)據(jù)),可以用Mutation通過提交(commit)來(lái)修改State。
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
}
},
actions: {
incrementAsync(context) {
setTimeout(() => {
context.commit('increment')
}, 1000)
}
}
})
5:modules
modules:用于將store分割成模塊,每個(gè)模塊都擁有自己的state、mutation、action、getters和子模塊,以便提高應(yīng)用程序的可維護(hù)性。
將State、Getter、Mutation、Action模塊化,便于組件化和模塊化開發(fā)。
const store = new Vuex.Store({
modules: {
cart: {
state: {
items: []
},
mutations: {
addItem(state, item) {
state.items.push(item)
}
},
actions: {
addAsyncItem(context, item) {
setTimeout(() => {
context.commit('addItem', item)
}, 1000)
}
}
}
}
})
使用方法示例:
const store = new Vuex.Store({
modules: {
cart: {
state: {
items: []
},
mutations: {
pushProductToCart (state, payload) {
state.items.push({
id: payload.id,
quantity: 1
})
}
},
actions: {
addProductToCart ({ state, commit }, product) {
const cartItem = state.items.find(item => item.id === product.id)
if (!cartItem) {
commit('pushProductToCart', product)
}
}
},
getters: {
cartItems: state => {
return state.items
}
}
}
}
})
這個(gè)代碼創(chuàng)建了一個(gè)包含cart模塊的Vuex store對(duì)象,其中cart模塊包含state、mutations、actions和getters四個(gè)屬性,用于管理購(gòu)物車數(shù)據(jù)。在addProductToCart action中,使用state.items和commit方法來(lái)修改cart模塊中的數(shù)據(jù)。在cartItems getter中,使用state.items來(lái)計(jì)算購(gòu)物車中的商品數(shù)量和總價(jià)。
三,Vuex使用方法
使用方法:
1:安裝Vuex:npm install vuex --save
2:在main.js中,導(dǎo)入Vuex,并使用Vue.use()方法注冊(cè)Vuex。
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
}
}
})
new Vue({
store,
render: h => h(App)
}).$mount('#app')
3:在組件中使用vuex中的數(shù)據(jù)和方法。
<template>
<div>
<p>Count: {{ count }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script>
export default {
computed: {
count() {
return this.$store.state.count
}
},
methods: {
increment() {
this.$store.commit('increment')
}
}
}
</script>
4:vuex綜合案例
下面是一個(gè)簡(jiǎn)單的Vuex使用方法的示例:
// 引入Vue和Vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 創(chuàng)建一個(gè)Store
const store = new Vuex.Store({
// 定義State
state: {
count: 0
},
// 定義Mutation
mutations: {
increment: state => state.count++,
decrement: state => state.count--
},
// 定義Getter
getters: {
evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
},
// 定義Action
actions: {
incrementIfOdd ({ commit, state }) {
if ((state.count + 1) % 2 === 0) {
commit('increment')
}
}
}
})
new Vue({
el: '#app',
store,
template: `
<div>
<p>Count: {{ count }}</p>
<p>Even or Odd? {{ evenOrOdd }}</p>
<button @click="increment">Increment</button>
<button @click="decrement">Decrement</button>
<button @click="incrementIfOdd">IncrementIfOdd</button>
</div>
`,
computed: {
count () {
return this.$store.state.count
},
evenOrOdd () {
return this.$store.getters.evenOrOdd
}
},
methods: {
increment () {
this.$store.commit('increment')
},
decrement () {
this.$store.commit('decrement')
},
incrementIfOdd () {
this.$store.dispatch('incrementIfOdd')
}
}
})
這個(gè)代碼創(chuàng)建了一個(gè)Vuex Store,并定義了State、Mutation、Getter、Action。然后將Store實(shí)例與Vue實(shí)例關(guān)聯(lián)。在Vue組件中,使用計(jì)算屬性(computed)和方法(methods)來(lái)訪問State、Getter和Action。在方法中,使用commit來(lái)提交Mutation,使用dispatch來(lái)分發(fā)Action。
總結(jié)
到此這篇關(guān)于vuex中的5個(gè)屬性使用方法的文章就介紹到這了,更多相關(guān)vuex的5個(gè)屬性使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn) 點(diǎn)擊顯示再點(diǎn)擊隱藏效果(點(diǎn)擊頁(yè)面空白區(qū)域也隱藏效果)
這篇文章主要介紹了Vue實(shí)現(xiàn) 點(diǎn)擊顯示 再點(diǎn)擊隱藏 點(diǎn)擊頁(yè)面空白區(qū)域也隱藏效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
vue用BMap百度地圖實(shí)現(xiàn)即時(shí)搜索功能
這篇文章主要為大家詳細(xì)介紹了vue用BMap百度地圖實(shí)現(xiàn)即時(shí)搜索功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
基于Vue實(shí)例對(duì)象的數(shù)據(jù)選項(xiàng)
下面小編就為大家?guī)?lái)一篇基于Vue實(shí)例對(duì)象的數(shù)據(jù)選項(xiàng)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-08-08
簡(jiǎn)化版的vue-router實(shí)現(xiàn)思路詳解
這篇文章主要介紹了簡(jiǎn)化版的vue-router,需要的朋友可以參考下2018-10-10
Vue.Draggable使用文檔超詳細(xì)總結(jié)
Vue拖拽組件(Draggable)是一個(gè)允許與View-Model同步進(jìn)行拖放排序的Vue組件,下面這篇文章主要給大家介紹了關(guān)于Vue.Draggable使用文檔的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Vue3中實(shí)現(xiàn)歌詞滾動(dòng)顯示效果
本文分享如何在Vue 3中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的歌詞滾動(dòng)效果,我將從歌詞數(shù)據(jù)的處理開始,一步步介紹布局的搭建和事件的實(shí)現(xiàn),感興趣的朋友跟隨小編一起看看吧2024-02-02

