Vue引入使用

Vuex3 和 Vue2.x

src/store/index.js

  1. import Vue from 'vue' // 2、引入vue
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)// 2、vue 使用 Vuex
  4. export default new Vuex.Store({
  5. state: {
  6. // 1、定义的Vuex公共变量(状态)
  7. a:100,
  8. b:"bb"
  9. },
  10. mutations: {
  11. },
  12. actions: {
  13. },
  14. modules: {
  15. }
  16. })

Vuex4 和 Vue3.x

src/main.js

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import router from './router/index.js'
  4. // 5、然后在 main.js中引入本文件的路径
  5. import store from './store/index.js'
  6. // 6、最后在 main.js 中.use(vuex) 让APP应用使用vuex
  7. createApp(App)
  8. .use(router)
  9. .use(store)
  10. .mount('#app');

========================

使用数据

其他组件页面可以直接使用,通过vue内置的$store 来调用

直接简单使用

Vue 2.x 或 Vue3 选项API

  1. <template>
  2. <div id="app">
  3. test1
  4. <!-- 通过这样的方式使用 -->
  5. {{$store.state.a}}
  6. <!-- 也可以通过computed计算属性使用 -->
  7. {{storeData}}
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. computed:{
  13. storeData(){
  14. return this.$store.state.a
  15. }
  16. },
  17. data() {
  18. return {
  19. data: {}
  20. };
  21. },
  22. };
  23. </script>

Vue3 组合API

  1. <template>
  2. <div>{{$store.state.a}} 可以直接获取</div>
  3. <div>{{b}} 也可以通过变量方式(推荐)</div>
  4. </template>
  5. <script>
  6. //7、在要使用vuex的.vue文件内,引入vuex的useStore方法
  7. import { useStore } from "vuex";
  8. import { computed } from 'vue';
  9. export default {
  10. setup() {
  11. //8、在setup()里面创建vuex实例
  12. const store = useStore()
  13. //9、通过computed使用,才是响应式的
  14. let a = computed(()=>{
  15. return store.state.a
  16. });
  17. let b = computed(()=>{
  18. return store.state.b
  19. });
  20. return {
  21. a,
  22. b
  23. }
  24. }
  25. }
  26. </script>

mapState 获取多个状态

image.png

Vue 2.x 或 Vue3 选项API

  1. <template>
  2. <div>
  3. <!-- 第一种:数组 -->
  4. {{a}}
  5. <!-- 第二种:对象 -->
  6. {{sB}}
  7. </div>
  8. </template>
  9. <script>
  10. import {mapState} from "vuex"
  11. export default {
  12. computed:{
  13. // mapState有两种方式,
  14. // 第一种参数是数组,数组元素就是数据仓库变量的名字
  15. // mapState会返回一个对象,对象每个参数都是一个函数,可以通过解构的方法,提取对象的内容,变成简单的变量
  16. ...mapState(["a","b"]),
  17. // 第二种参数是对象,可以自定义要展示的名字
  18. ...mapState({
  19. sA:state => state.a,
  20. sB:state => state.b,
  21. }),
  22. // 相当于
  23. sA(){
  24. return this.$store.state.a
  25. },
  26. sB(){
  27. return this.$store.state.b
  28. },
  29. },
  30. data() {
  31. return {
  32. data: {}
  33. };
  34. },
  35. };
  36. </script>

Vue3 组合API

简单的,就是多写几个上面介绍的vue3组合api的就可以了,然后return出去。

如果不想写那么多个,官方没有很好的方法直接把mapState放在setup里面使用。

需要我们自己通过js操作一下,下面是老师CoderWHY的建议:

  1. <template>
  2. <div>{{$store.state.a}} 可以直接获取</div>
  3. <div>{{b}} 也可以通过变量方式(推荐)</div>
  4. </template>
  5. <script>
  6. import { useStore , mapState } from "vuex";
  7. import { computed } from 'vue';
  8. export default {
  9. setup() {
  10. // 1、引入vuex的hook函数
  11. const store = useStore()
  12. // 2、使用mapState 获取数据仓库的变量
  13. /* storeState = {
  14. a:function(){return this.$store.state.a},
  15. b:function(){return this.$store.state.b},
  16. }
  17. */
  18. const storeState = mapState(["a","b"])
  19. // 3、通过js处理mapState
  20. const storeObj = {}
  21. /*
  22. Object.keys(storeState) = [a,b]
  23. */
  24. Object.keys(storeState).forEach(key => {
  25. /*
  26. fn = function(){return this.$store.state.a} , 在setup里面,this是undefined。
  27. 函数().bind(对象)是给函数绑定一个对象,让函数的this指向这个对象。
  28. 现在给fn绑定一个对象{$store:store},store就是上面useStore获得的对象。
  29. 因此:fn = function(){return store.state.a}
  30. */
  31. const fn = storeState[key].bind({$store:store})
  32. /*
  33. storeObj[a] = storeObj.a = computed(function(){return store.state.a})
  34. */
  35. storeObj[key] = computed(fn)
  36. })
  37. return {
  38. /*
  39. 相当于:
  40. a, // a 为 computed(function(){return store.state.a})
  41. b, // b 为 computed(function(){return store.state.b})
  42. */
  43. ...storeObj
  44. }
  45. }
  46. }
  47. </script>

中间转化的过程可以封装成一个hook函数,在要使用的组件中,引入,然后直接使用即可。
image.png
image.png