3.1 vuex概念

https://vuex.vuejs.org/zh/installation.html

vuex 是 Vue 配套的 公共数据管理工具,它可以把一些共享的数据保存到 vuex 中,方便 整个程序中的任何组件直接获取或修改我们的公共数据;

3.2 vuex的基本使用

  1. #1.拷贝原项目中除了node_modules之外的其他信息,运行npm install安装模块
  2. #2.安装vuex
  3. npm i vuex -S
  4. #3.main.js
  5. import Vue from 'vue'
  6. import Vuex from 'vuex'
  7. // 注册vuex到vue中
  8. Vue.use(Vuex)
  9. // new Vuex.Store()实例,得到一个数据仓储对象
  10. // 可以在组件中通过this.$store.state.xx 来访问store中的数据
  11. var store = new Vuex.Store({
  12. //state相当于组件中的data
  13. state: {
  14. count: 0
  15. },
  16. //如果要修改store中state的值,需要调用 mutations提供的方法,可以通过this.$store.commit('方法名')来调用
  17. mutations: {
  18. increment(state) {
  19. state.count++
  20. },
  21. //mutations函数参数列表中最多支持两个参数,其中参数1是state; 参数2是通过commit提交过来的参数;
  22. subtract(state, obj) {
  23. console.log(obj)
  24. state.count -= obj.step;
  25. }
  26. },
  27. getters: {
  28. //这里的getters只负责对外提供数据,不负责修改数据,如果想要修改 state 中的数据需要在mutations中修改
  29. optCount: function (state) {
  30. return '当前最新的count值是:' + state.count
  31. }
  32. }
  33. })
  34. // 总结:
  35. // 1. state中的数据,不能直接修改,如果想要修改,必须通过 mutations
  36. // 2. 如果组件想要直接 从 state 上获取数据: 需要 this.$store.state.***
  37. // 3. 如果组件想要修改数据,必须使用 mutations 提供的方法,需要通过 this.$store.commit('方法的名称', 唯一的一个参数)
  38. // 4. store中state上的数据在对外提供的时候建议做一层包装,推荐使用 getters。调用的时候则用this.$store.getters.***
  39. import App from './App.vue'
  40. const vm = new Vue({
  41. el: '#app',
  42. render: c => c(App),
  43. //将vuex创建的store挂载到VM实例上,只要挂载到了 vm 上,任何组件都能使用store来存取数据
  44. store
  45. })
  46. #4.index.html
  47. <body>
  48. <div id="app"></div>
  49. </body>
  50. #5.App.vue
  51. <template>
  52. <div>
  53. <h1>这是 App 组件</h1>
  54. <hr>
  55. <counter></counter>
  56. <hr>
  57. <amount></amount>
  58. </div>
  59. </template>
  60. <script>
  61. import counter from "./components/counter.vue";
  62. import amount from "./components/amount.vue";
  63. export default {
  64. data() {
  65. return {};
  66. },
  67. components: {
  68. counter,
  69. amount
  70. }
  71. };
  72. </script>
  73. #6.components/amount.vue
  74. <template>
  75. <div>
  76. <h3>{{ $store.getters.optCount }}</h3>
  77. </div>
  78. </template>
  79. #7.components/counter.vue
  80. <template>
  81. <div>
  82. <input type="button" value="绑定事件-减少" @click="sub">
  83. <input type="button" value="绑定事件-增加" @click="add">
  84. <br>
  85. <input type="text" v-model="$store.state.count">
  86. </div>
  87. </template>
  88. <script>
  89. export default {
  90. data() {
  91. return {
  92. };
  93. },
  94. methods: {
  95. add() {
  96. this.$store.commit("increment");
  97. },
  98. sub() {
  99. this.$store.commit("subtract",{ step:3});
  100. }
  101. }
  102. };
  103. </script>

3.3 vuex异步修改状态

mutation的使用局限:mutation必须是同步函数,如果有异步操作,可以在actions完成

  1. #main.js中给store添加actions的方法
  2. var store = new Vuex.Store({
  3. // state相当于组件中的data
  4. state: {
  5. count: 0
  6. },
  7. // 如果要修改store中state的值,需要调用 mutations提供的方法,可以通过this.$store.commit('方法名')来调用
  8. mutations: {
  9. increment (state) {
  10. state.count++
  11. },
  12. // mutations函数参数列表中最多支持两个参数,其中参数1是state; 参数2是通过commit提交过来的参数;
  13. subtract (state, obj) {
  14. console.log(obj)
  15. state.count -= obj.step
  16. }
  17. },
  18. getters: {
  19. // 这里的getters只负责对外提供数据,不负责修改数据,如果想要修改 state 中的数据需要在mutations中修改
  20. optCount: function (state) {
  21. return '当前最新的count值是:' + state.count
  22. }
  23. },
  24. actions: {
  25. // action中的方法允许异步事件
  26. increment ({ commit }) {
  27. setTimeout(() => {
  28. // 提交mutations中的increment方法
  29. commit('increment')
  30. }, 1000)
  31. },
  32. subtract ({ commit }, obj) {
  33. // 返回一个Promise,以便做回调
  34. return new Promise((resolve, reject) => {
  35. setTimeout(() => {
  36. commit('subtract', obj)
  37. resolve()
  38. }, 1000)
  39. })
  40. }
  41. }
  42. })
  43. #components/counter.vue
  44. methods: {
  45. add () {
  46. // dispatch 触发指定的action
  47. this.$store.dispatch("increment")
  48. },
  49. sub () {
  50. this.$store.dispatch("subtract", { step: 3 })
  51. .then(() => {
  52. console.log("减少操作完成")
  53. })
  54. }
  55. }

3.4 vuex的模块

  1. #1.使用Vuex.Store中的modules声明多个模块
  2. const moduleA = {
  3. state: {
  4. count: 1
  5. },
  6. mutations: {
  7. }
  8. }
  9. const moduleB = {
  10. state: {
  11. count: 11
  12. },
  13. mutations: {
  14. }
  15. }
  16. const store = new Vuex.Store({
  17. modules: {
  18. a: moduleA,
  19. b: moduleB
  20. }
  21. })
  22. #2.使用不同模块中的数据
  23. <div>
  24. <h3>{{ $store.state.a.count }}</h3>
  25. <h3>{{ $store.state.b.count }}</h3>
  26. </div>

3.5 刷新网页时vuex缓存

https://blog.csdn.net/weixin_42233917/article/details/82217596