欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vuex3和Vuex4有哪些區(qū)別

 更新時(shí)間:2023年04月20日 10:22:07   作者:shichuan  
本文主要介紹了Vuex3和Vuex4有哪些區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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:使用 mapStatemapGetters、mapMutationsmapActions 輔助函數(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)聽和更新。
  • Vuex 4:使用 Vue 3 的響應(yīng)式系統(tǒng) ( proxy ) 進(jìn)行狀態(tài)的監(jiān)聽和更新,可以利用 Composition API 中的 reactivecomputed 函數(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ò)展。

參考鏈接:

到此這篇關(guān)于Vuex3和Vuex4有哪些區(qū)別的文章就介紹到這了,更多相關(guān)Vuex3和Vuex4內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論