vuex中的5個屬性使用方法舉例講解
一,Vuex簡介
Vuex是Vue.js的狀態(tài)管理庫,它通過中心化的狀態(tài)管理使得組件間的數(shù)據(jù)共享更加容易。
Vuex包含五個核心屬性:state、getters、mutations、actions和modules。
Vuex是Vue.js的狀態(tài)管理庫,它提供了一種集中式存儲管理應用程序中所有組件的狀態(tài),并將其分離到一個可預測的狀態(tài)容器中。Vuex包括五個核心屬性:
二,Vuex五個核心屬性
1:state
state:定義了應用程序的狀態(tài),就是我們要管理的數(shù)據(jù)。
存放應用程序的狀態(tài)(數(shù)據(jù)),所有組件共享。它是Vue實例的data屬性的替代品,但是通過它存儲和管理的狀態(tài),可以在整個應用程序中實現(xiàn)全局共享,即不同的組件可以通過定義的getter和setter訪問同一狀態(tài)數(shù)據(jù)。
const store = new Vuex.Store({
state: {
count: 0
}
})
2:getters
getters:用于獲取State中的狀態(tài),主要用于對state進行邏輯上的組合和應用,類似于Vue組件中的計算屬性。
const store = new Vuex.Store({
state: {
count: 0
},
getters: {
doubleCount(state) {
return state.count * 2
}
}
})
3:mutations
mutations:用于修改state中的數(shù)據(jù),是唯一可以修改state的地方。mutations接收state作為第一個參數(shù),接收payload作為第二個參數(shù)。
用于修改State中的狀態(tài),只能同步執(zhí)行。Mutation必須是同步函數(shù),因為它們不能處理異步行為,異步行為應該放在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中可以進行任何異步操作,最后再提交到mutations中同步修改state。actions接收context作為第一個參數(shù),其中包含了state、getters和commit等屬性。
可以包含任意異步操作(例如從服務器獲取數(shù)據(jù)),可以用Mutation通過提交(commit)來修改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分割成模塊,每個模塊都擁有自己的state、mutation、action、getters和子模塊,以便提高應用程序的可維護性。
將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
}
}
}
}
})
這個代碼創(chuàng)建了一個包含cart模塊的Vuex store對象,其中cart模塊包含state、mutations、actions和getters四個屬性,用于管理購物車數(shù)據(jù)。在addProductToCart action中,使用state.items和commit方法來修改cart模塊中的數(shù)據(jù)。在cartItems getter中,使用state.items來計算購物車中的商品數(shù)量和總價。
三,Vuex使用方法
使用方法:
1:安裝Vuex:npm install vuex --save
2:在main.js中,導入Vuex,并使用Vue.use()方法注冊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綜合案例
下面是一個簡單的Vuex使用方法的示例:
// 引入Vue和Vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 創(chuàng)建一個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')
}
}
})
這個代碼創(chuàng)建了一個Vuex Store,并定義了State、Mutation、Getter、Action。然后將Store實例與Vue實例關(guān)聯(lián)。在Vue組件中,使用計算屬性(computed)和方法(methods)來訪問State、Getter和Action。在方法中,使用commit來提交Mutation,使用dispatch來分發(fā)Action。
總結(jié)
到此這篇關(guān)于vuex中的5個屬性使用方法的文章就介紹到這了,更多相關(guān)vuex的5個屬性使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實現(xiàn) 點擊顯示再點擊隱藏效果(點擊頁面空白區(qū)域也隱藏效果)
這篇文章主要介紹了Vue實現(xiàn) 點擊顯示 再點擊隱藏 點擊頁面空白區(qū)域也隱藏效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01

