Vuex3和Vuex4的区别是什么

前端开发   发布日期:2025年03月22日   浏览次数:255

这篇文章主要讲解了“Vuex3和Vuex4的区别是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Vuex3和Vuex4的区别是什么”吧!

Vuex 是 Vue.js 的官方状态管理库,用于在 Vue.js 应用中管理应用状态。Vuex 3 是用于 Vue 2 的版本,而 Vuex 4 是用于 Vue 3 的版本。下面是 Vuex 3 和 Vuex 4 在一些重要方面的异同点:

创建 Store 的方式

  • Vuex 3:使用

    1. new Vuex.Store()
    创建 store 实例
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. const store = new Vuex.Store({
  5. // 配置项
  6. })
  7. export default store
  • Vuex 4:使用

    1. createStore
    函数创建 store 实例
  1. import { createStore } from 'vuex'
  2. const store = createStore({
  3. // 配置项
  4. })
  5. export default store

Vuex 4 中使用

  1. createStore
函数来创建 store 实例,而不是直接在 Vue 实例上挂载。

在组件中使用 Store

  • Vuex 3:使用

    1. this.$store
    访问 store 实例,通过
    1. this.$store.state
    访问状态,通过
    1. this.$store.commit()
    进行提交 mutation,通过
    1. this.$store.dispatch()
    进行分发 action。
  1. export default {
  2. computed: {
  3. count() {
  4. return this.$store.state.count
  5. }
  6. },
  7. methods: {
  8. increment() {
  9. this.$store.commit('increment')
  10. },
  11. incrementAsync() {
  12. this.$store.dispatch('incrementAsync')
  13. }
  14. }
  15. }
  • Vuex 4:使用

    1. useStore
    函数来获取 store 实例,通过
    1. store.state
    访问状态,通过
    1. store.commit()
    进行提交 mutation,通过
    1. store.dispatch()
    进行分发 action。
  1. import { useStore } from 'vuex'
  2. export default {
  3. setup() {
  4. const store = useStore()
  5. const count = computed(() => store.state.count)
  6. const increment = () => {
  7. store.commit('increment')
  8. }
  9. const incrementAsync = () => {
  10. store.dispatch('incrementAsync')
  11. }
  12. return {
  13. count,
  14. increment,
  15. incrementAsync
  16. }
  17. }
  18. }

虽然 Vuex4 推荐使用更符合 Composition API 风格的

  1. useStore()
来获取
  1. store
实例。但是并没有移除
  1. this.$store
,但是在
  1. <template>
  1. Vue2
选项式写法中还是支持使用
  1. $store
的。

辅助函数的用法

  • Vuex 3:使用

    1. mapState
    1. mapGetters
    1. mapMutations
    1. mapActions
    辅助函数来进行映射,简化在组件中对 store 的访问。
  1. import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
  2. export default {
  3. computed: {
  4. ...mapState(['count']),
  5. ...mapGetters(['doubleCount']),
  6. },
  7. methods: {
  8. ...mapMutations(['increment']),
  9. ...mapActions(['incrementAsync']),
  10. }
  11. }
  • Vuex 4:使用 Composition API 中的

    1. computed
    函数和普通的 JavaScript 函数来实现类似的功能。
  1. import { computed, useStore } from 'vuex'
  2. export default {
  3. setup() {
  4. const store = useStore()
  5. const count = computed(() => store.state.count)
  6. const doubleCount = computed(() => store.getters.doubleCount)
  7. const increment = () => {
  8. store.commit('increment')
  9. }
  10. const incrementAsync = () => {
  11. store.dispatch('incrementAsync')
  12. }
  13. return {
  14. count,
  15. doubleCount,
  16. increment,
  17. incrementAsync
  18. }
  19. }
  20. }

Vuex4 支持选项式写法的辅助函数,在使用时和 Vuex3 一模一样的。但是需要注意辅助函数不能在组合式写法

  1. setup
中使用。

响应式的改进

  • Vuex 3:使用 Vue 2 的响应式系统 ( Object.defineProperty ) 进行状态的监听和更新。

  • Vuex 4:使用 Vue 3 的响应式系统 ( proxy ) 进行状态的监听和更新,可以利用 Composition API 中的

    1. reactive
    1. computed
    函数进行更加灵活和高效的状态管理。

实质上这是 Vue2 和 Vue3 的区别,只是由于 Vue 2 匹配的 Vuex 3,Vue 3 匹配的 Vuex 4 的原因,严格来说不能算作 Vuex3 与 Vuex4 的不同。

Vuex4 支持多例模式

Vuex 3 是单例模式的,即整个应用只能有一个全局的 Vuex Store 实例。而在 Vuex 4 中,你可以通过

  1. useStore
函数在不同组件中创建多个独立的 Vuex Store 实例,从而支持多例模式。

以下是一个示例展示了如何在 Vuex 4 中使用

  1. useStore
辅助函数创建多个独立的 Vuex Store 实例:
  1. <template>
  2. <div>
  3. <p>Counter 1: {{ counter1 }}</p>
  4. <p>Counter 2: {{ counter2 }}</p>
  5. <button @click="incrementCounter1">Increment Counter 1</button>
  6. <button @click="incrementCounter2">Increment Counter 2</button>
  7. </div>
  8. </template>
  9. <script>
  10. import { useStore } from 'vuex'
  11. export default {
  12. setup() {
  13. // 使用 useStore 辅助函数创建 Vuex Store 实例
  14. const store1 = useStore('store1')
  15. const store2 = useStore('store2')
  16. // 通过 store1.state.count 获取第一个 Store 的状态
  17. const count1 = store1.state.count
  18. // 通过 store2.state.count 获取第二个 Store 的状态
  19. const count2 = store2.state.count
  20. // 通过 store1.commit() 提交 mutations 到第一个 Store
  21. const incrementCounter1 = () => {
  22. store1.commit('increment')
  23. }
  24. // 通过 store2.commit() 提交 mutations 到第二个 Store
  25. const incrementCounter2 = () => {
  26. store2.commit('increment')
  27. }
  28. return {
  29. count1,
  30. count2,
  31. incrementCounter1,
  32. incrementCounter2
  33. }
  34. }
  35. }
  36. </script>

上述示例展示了如何在 Vue 组件中使用

  1. useStore
辅助函数创建多个独立的 Vuex Store 实例,并通过这些实例分别访问和修改各自的状态和 mutations。这是 Vuex 4 相对于 Vuex 3 的一个重要的改进,使得 Vuex 在支持多例模式的场景下更加灵活和可扩展。

以上就是Vuex3和Vuex4的区别是什么的详细内容,更多关于Vuex3和Vuex4的区别是什么的资料请关注九品源码其它相关文章!