Vuex3和Vuex4有哪些区别

 更新时间:2023年04月20日 10:22:07   作者:shichuan  
本文主要介绍了Vuex3和Vuex4有哪些区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

创建 Store 的方式

  • Vuex 3:使用 new Vuex.Store() 创建 store 实例
import Vue from 'vue'
import Vuex from 'vuex'
​
Vue.use(Vuex)
​
const store = new Vuex.Store({
  // 配置项
})
​
export default store
  • Vuex 4:使用 createStore 函数创建 store 实例
import { createStore } from 'vuex'
​
const store = createStore({
  // 配置项
})
​
export default store

Vuex 4 中使用 createStore 函数来创建 store 实例,而不是直接在 Vue 实例上挂载。

在组件中使用 Store

  • Vuex 3:使用 this.$store 访问 store 实例,通过 this.$store.state 访问状态,通过 this.$store.commit() 进行提交 mutation,通过 this.$store.dispatch() 进行分发 action。
export default {
  computed: {
    count() {
      return this.$store.state.count
    }
  },
  methods: {
    increment() {
      this.$store.commit('increment')
    },
    incrementAsync() {
      this.$store.dispatch('incrementAsync')
    }
  }
}
  • Vuex 4:使用 useStore 函数来获取 store 实例,通过 store.state 访问状态,通过 store.commit() 进行提交 mutation,通过 store.dispatch() 进行分发 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 风格的 useStore() 来获取 store 实例。但是并没有移除 this.$store,但是在 <template>Vue2 选项式写法中还是支持使用 $store 的。

辅助函数的用法

  • Vuex 3:使用 mapStatemapGettersmapMutationsmapActions 辅助函数来进行映射,简化在组件中对 store 的访问。
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
​
export default {
  computed: {
    ...mapState(['count']),
    ...mapGetters(['doubleCount']),
  },
  methods: {
    ...mapMutations(['increment']),
    ...mapActions(['incrementAsync']),
  }
}
  • Vuex 4:使用 Composition API 中的 computed 函数和普通的 JavaScript 函数来实现类似的功能。
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 支持选项式写法的辅助函数,在使用时和 Vuex3 一模一样的。但是需要注意辅助函数不能在组合式写法 setup 中使用。

响应式的改进

  • Vuex 3:使用 Vue 2 的响应式系统 ( Object.defineProperty ) 进行状态的监听和更新。
  • Vuex 4:使用 Vue 3 的响应式系统 ( proxy ) 进行状态的监听和更新,可以利用 Composition API 中的 reactivecomputed 函数进行更加灵活和高效的状态管理。

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

Vuex4 支持多例模式

Vuex 3 是单例模式的,即整个应用只能有一个全局的 Vuex Store 实例。而在 Vuex 4 中,你可以通过 useStore 函数在不同组件中创建多个独立的 Vuex Store 实例,从而支持多例模式。

以下是一个示例展示了如何在 Vuex 4 中使用 useStore 辅助函数创建多个独立的 Vuex Store 实例:

<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 辅助函数创建 Vuex Store 实例
    const store1 = useStore('store1')
    const store2 = useStore('store2')
​
    // 通过 store1.state.count 获取第一个 Store 的状态
    const count1 = store1.state.count
    // 通过 store2.state.count 获取第二个 Store 的状态
    const count2 = store2.state.count
​
    // 通过 store1.commit() 提交 mutations 到第一个 Store
    const incrementCounter1 = () => {
      store1.commit('increment')
    }
​
    // 通过 store2.commit() 提交 mutations 到第二个 Store
    const incrementCounter2 = () => {
      store2.commit('increment')
    }
​
    return {
      count1,
      count2,
      incrementCounter1,
      incrementCounter2
    }
  }
}
</script>

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

参考链接:

到此这篇关于Vuex3和Vuex4有哪些区别的文章就介绍到这了,更多相关Vuex3和Vuex4内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue中的组件详谈

    Vue中的组件详谈

    这篇文章主要介绍了Vue的组件,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • Vue组件化学习之scoped详解

    Vue组件化学习之scoped详解

    这篇文章主要为大家详细介绍了Vue组件化学习之scoped,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • 详解vue.js组件化开发实践

    详解vue.js组件化开发实践

    本篇文章主要介绍了vue.js组件化开发实践,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • vue项目中自定义video视频控制条的实现代码

    vue项目中自定义video视频控制条的实现代码

    这篇文章主要介绍了vue项目中自定义video视频控制条的实现代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • vue中关于element的el-image 图片预览功能增加一个下载按钮(操作方法)

    vue中关于element的el-image 图片预览功能增加一个下载按钮(操作方法)

    这篇文章主要介绍了vue中关于element的el-image 图片预览功能增加一个下载按钮,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • Vue+ElementUI技巧之自定义表单项label的文字提示方法

    Vue+ElementUI技巧之自定义表单项label的文字提示方法

    这篇文章主要给大家介绍了关于Vue+ElementUI技巧之自定义表单项label文字提示的相关资料,文中通过图文以及代码示例介绍的非常详细,对大家的学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-02-02
  • 使用vNode实现给列表字段打标签

    使用vNode实现给列表字段打标签

    这篇文章主要为大家介绍了使用vNode实现给列表字段打标签示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Vue+ElementUI实现从后台动态填充下拉框的示例代码

    Vue+ElementUI实现从后台动态填充下拉框的示例代码

    本文主要介绍了Vue+ElementUI实现从后台动态填充下拉框的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • vue-element-admin配置小结

    vue-element-admin配置小结

    本文主要介绍了vue-element-admin配置小结,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Vue中provide、inject详解以及使用教程

    Vue中provide、inject详解以及使用教程

    provide和inject主要为高阶插件/组件库提供用例,并不推荐直接用于应用程序代码中,下面这篇文章主要给大家介绍了关于Vue中provide、inject详解以及使用的相关资料,需要的朋友可以参考下
    2022-11-11

最新评论