Vuex3和Vuex4有哪些區(qū)別
Vuex 是 Vue.js 的官方狀態(tài)管理庫(kù),用于在 Vue.js 應(yīng)用中管理應(yīng)用狀態(tài)。Vuex 3 是用于 Vue 2 的版本,而 Vuex 4 是用于 Vue 3 的版本。下面是 Vuex 3 和 Vuex 4 在一些重要方面的異同點(diǎn):
創(chuàng)建 Store 的方式
- Vuex 3:使用
new Vuex.Store()創(chuàng)建 store 實(shí)例
import Vue from 'vue'
import Vuex from 'vuex'
?
Vue.use(Vuex)
?
const store = new Vuex.Store({
// 配置項(xiàng)
})
?
export default store
- Vuex 4:使用
createStore函數(shù)創(chuàng)建 store 實(shí)例
import { createStore } from 'vuex'
?
const store = createStore({
// 配置項(xiàng)
})
?
export default store
Vuex 4 中使用 createStore 函數(shù)來(lái)創(chuàng)建 store 實(shí)例,而不是直接在 Vue 實(shí)例上掛載。
在組件中使用 Store
- Vuex 3:使用
this.$store訪問(wèn) store 實(shí)例,通過(guò)this.$store.state訪問(wèn)狀態(tài),通過(guò)this.$store.commit()進(jìn)行提交 mutation,通過(guò)this.$store.dispatch()進(jìn)行分發(fā) action。
export default {
computed: {
count() {
return this.$store.state.count
}
},
methods: {
increment() {
this.$store.commit('increment')
},
incrementAsync() {
this.$store.dispatch('incrementAsync')
}
}
}
- Vuex 4:使用
useStore函數(shù)來(lái)獲取 store 實(shí)例,通過(guò)store.state訪問(wèn)狀態(tài),通過(guò)store.commit()進(jìn)行提交 mutation,通過(guò)store.dispatch()進(jìn)行分發(fā) action。
import { useStore } from 'vuex'
?
export default {
setup() {
const store = useStore()
const count = computed(() => store.state.count)
?
const increment = () => {
store.commit('increment')
}
?
const incrementAsync = () => {
store.dispatch('incrementAsync')
}
?
return {
count,
increment,
incrementAsync
}
}
}
雖然 Vuex4 推薦使用更符合 Composition API 風(fēng)格的 useStore() 來(lái)獲取 store 實(shí)例。但是并沒(méi)有移除 this.$store,但是在 <template> 和 Vue2 選項(xiàng)式寫法中還是支持使用 $store 的。
輔助函數(shù)的用法
- Vuex 3:使用
mapState、mapGetters、mapMutations和mapActions輔助函數(shù)來(lái)進(jìn)行映射,簡(jiǎn)化在組件中對(duì) store 的訪問(wèn)。
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
?
export default {
computed: {
...mapState(['count']),
...mapGetters(['doubleCount']),
},
methods: {
...mapMutations(['increment']),
...mapActions(['incrementAsync']),
}
}
- Vuex 4:使用 Composition API 中的
computed函數(shù)和普通的 JavaScript 函數(shù)來(lái)實(shí)現(xiàn)類似的功能。
import { computed, useStore } from 'vuex'
?
export default {
setup() {
const store = useStore()
?
const count = computed(() => store.state.count)
const doubleCount = computed(() => store.getters.doubleCount)
?
const increment = () => {
store.commit('increment')
}
?
const incrementAsync = () => {
store.dispatch('incrementAsync')
}
?
return {
count,
doubleCount,
increment,
incrementAsync
}
}
}
Vuex4 支持選項(xiàng)式寫法的輔助函數(shù),在使用時(shí)和 Vuex3 一模一樣的。但是需要注意輔助函數(shù)不能在組合式寫法 setup 中使用。
響應(yīng)式的改進(jìn)
- Vuex 3:使用 Vue 2 的響應(yīng)式系統(tǒng) ( Object.defineProperty ) 進(jìn)行狀態(tài)的監(jiān)聽(tīng)和更新。
- Vuex 4:使用 Vue 3 的響應(yīng)式系統(tǒng) ( proxy ) 進(jìn)行狀態(tài)的監(jiān)聽(tīng)和更新,可以利用 Composition API 中的
reactive和computed函數(shù)進(jìn)行更加靈活和高效的狀態(tài)管理。
實(shí)質(zhì)上這是 Vue2 和 Vue3 的區(qū)別,只是由于 Vue 2 匹配的 Vuex 3,Vue 3 匹配的 Vuex 4 的原因,嚴(yán)格來(lái)說(shuō)不能算作 Vuex3 與 Vuex4 的不同。
Vuex4 支持多例模式
Vuex 3 是單例模式的,即整個(gè)應(yīng)用只能有一個(gè)全局的 Vuex Store 實(shí)例。而在 Vuex 4 中,你可以通過(guò) useStore 函數(shù)在不同組件中創(chuàng)建多個(gè)獨(dú)立的 Vuex Store 實(shí)例,從而支持多例模式。
以下是一個(gè)示例展示了如何在 Vuex 4 中使用 useStore 輔助函數(shù)創(chuàng)建多個(gè)獨(dú)立的 Vuex Store 實(shí)例:
<template>
<div>
<p>Counter 1: {{ counter1 }}</p>
<p>Counter 2: {{ counter2 }}</p>
<button @click="incrementCounter1">Increment Counter 1</button>
<button @click="incrementCounter2">Increment Counter 2</button>
</div>
</template>
?
<script>
import { useStore } from 'vuex'
?
export default {
setup() {
// 使用 useStore 輔助函數(shù)創(chuàng)建 Vuex Store 實(shí)例
const store1 = useStore('store1')
const store2 = useStore('store2')
?
// 通過(guò) store1.state.count 獲取第一個(gè) Store 的狀態(tài)
const count1 = store1.state.count
// 通過(guò) store2.state.count 獲取第二個(gè) Store 的狀態(tài)
const count2 = store2.state.count
?
// 通過(guò) store1.commit() 提交 mutations 到第一個(gè) Store
const incrementCounter1 = () => {
store1.commit('increment')
}
?
// 通過(guò) store2.commit() 提交 mutations 到第二個(gè) Store
const incrementCounter2 = () => {
store2.commit('increment')
}
?
return {
count1,
count2,
incrementCounter1,
incrementCounter2
}
}
}
</script>
上述示例展示了如何在 Vue 組件中使用 useStore 輔助函數(shù)創(chuàng)建多個(gè)獨(dú)立的 Vuex Store 實(shí)例,并通過(guò)這些實(shí)例分別訪問(wèn)和修改各自的狀態(tài)和 mutations。這是 Vuex 4 相對(duì)于 Vuex 3 的一個(gè)重要的改進(jìn),使得 Vuex 在支持多例模式的場(chǎng)景下更加靈活和可擴(kuò)展。
參考鏈接:
- Vuex 4 官方文檔:vuex.vuejs.org/zh/
- Vuex 3 官方文檔:v3.vuex.vuejs.org/zh/
到此這篇關(guān)于Vuex3和Vuex4有哪些區(qū)別的文章就介紹到這了,更多相關(guān)Vuex3和Vuex4內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue在 Nuxt.js 中重定向 404 頁(yè)面的方法
這篇文章主要介紹了Vue在 Nuxt.js 中重定向 404 頁(yè)面的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
elementPlus中的Autocomplete彈出層錯(cuò)位問(wèn)題解決分析
這篇文章主要介紹了elementPlus中的Autocomplete彈出層錯(cuò)位問(wèn)題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
詳解在vue中如何實(shí)現(xiàn)屏幕錄制與直播推流功能
屏幕錄制和直播推流是現(xiàn)代Web應(yīng)用中常用的功能,Vue作為一種流行的JavaScript框架,提供了一些工具和庫(kù),可以方便地實(shí)現(xiàn)屏幕錄制和直播推流功能,本文將介紹如何在Vue中進(jìn)行屏幕錄制和直播推流,需要的朋友可以參考下2024-01-01
vue 輸入電話號(hào)碼自動(dòng)按3-4-4分割功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 輸入電話號(hào)碼自動(dòng)按3-4-4分割功能的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Vue+ElementUI項(xiàng)目使用webpack輸出MPA的方法
這篇文章主要介紹了Vue+ElementUI項(xiàng)目使用webpack輸出MPA的方法,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
vue2使用el-date-picker實(shí)現(xiàn)動(dòng)態(tài)日期范圍demo
這篇文章主要為大家介紹了vue2使用el-date-picker實(shí)現(xiàn)動(dòng)態(tài)日期范圍示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Vue cli+mui 區(qū)域滾動(dòng)的實(shí)例代碼
下面小編就為大家分享一篇Vue cli+mui 區(qū)域滾動(dòng)的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
vue中el-cascader三級(jí)聯(lián)動(dòng)懶加載回顯問(wèn)題解決
本文主要介紹了vue中el-cascader三級(jí)聯(lián)動(dòng)懶加載回顯問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

