官方API地址:https://vuex.vuejs.org/zh/guide/modules.html

前面几节课写的user.js就称为一个module,这样做的原因是:由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。每个模块又是独立的store,拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割。

下面以在根store对象动态注册模块为例来对module做演示:

store.vue代码:

  1. <template>
  2. <div>
  3. <a-input :value="inputValue" @input="handlerInput"></a-input>
  4. <p>{{ inputValue }} -> lastLetter is {{ inputValueLastLetter }}</p>
  5. <p>appName: {{ appName }}, appNameWithVersion : {{ appNameWithVersion }}</p>
  6. <p>userName : {{ userName }}, firstLetter is : {{ firstLetter }}</p>
  7. <button @click="handleChangeAppName">修改appName和user.js中的userName</button>
  8. <p>动态给state增加appVersion: {{ appVersion }}</p>
  9. <button @click="handleActionChangeAppName">通过Action修改appName</button>
  10. <button @click="registerModule">动态注册模块</button>
  11. <p v-for="(li, index) in todoList" :key="index">{{ li }}</p>
  12. </div>
  13. </template>
  14. <script>
  15. import AInput from "_c/AInput.vue";
  16. import AShow from "_c/AShow.vue";
  17. //变量的解构赋值
  18. import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
  19. import { stat } from "fs";
  20. export default {
  21. name: "store",
  22. data() {
  23. return {
  24. inputValue: ""
  25. };
  26. },
  27. components: {
  28. AInput: AInput,
  29. AShow: AShow
  30. },
  31. computed: {
  32. //ES6展开操作符 mapState展开会形成一个对象 使用对象展开运算符将此对象混入到外部对象中
  33. ...mapState({
  34. appName: state => state.appName,
  35. appVersion: state => state.appVersion,
  36. userName: state => state.user.userName,
  37. todoList: state => (state.todo ? state.todo.todoList : [])
  38. }),
  39. // 使用对象展开运算符将 getter 混入 computed 对象中
  40. // ...mapGetters(["appNameWithVersion"]),
  41. appNameWithVersion() {
  42. //通过属性访问getters,Getter 会暴露为 store.getters 对象,可以以属性的形式访问这些值:
  43. return this.$store.getters.appNameWithVersion;
  44. },
  45. ...mapGetters(["firstLetter"]),
  46. inputValueLastLetter() {
  47. return this.inputValue.substr(-1, 1);
  48. }
  49. },
  50. methods: {
  51. handlerInput(val) {
  52. this.inputValue = val;
  53. },
  54. //
  55. ...mapMutations([
  56. "SET_USER_NAME", //将 `this.SET_USER_NAME()` 映射为 `this.$store.commit('SET_USER_NAME')`
  57. "SET_APP_NAME" //将 `this.SET_APP_NAME()` 映射为 `this.$store.commit('SET_APP_NAME')`
  58. ]),
  59. ...mapActions([
  60. "updateAppName" //将 `this.updateAppName()` 映射为 `this.$store.dispatch('updateAppName')`
  61. ]),
  62. handleChangeAppName() {
  63. this.SET_APP_NAME({
  64. appName: "newAppName"
  65. });
  66. this.SET_USER_NAME({
  67. userName: "shuyujie"
  68. });
  69. this.$store.commit("SET_APP_VERSION");
  70. },
  71. handleActionChangeAppName() {
  72. //第一种调用Action的方法
  73. //this.$store.dispatch('updateAppName')
  74. //第二种调用Action的方法
  75. this.updateAppName();
  76. },
  77. registerModule() {
  78. this.$store.registerModule( "todo", {
  79. state: {
  80. todoList: ["学习mutations", "学习actions"]
  81. }
  82. });
  83. }
  84. }
  85. };
  86. </script>

效果图:

若给user动态一个模块,则store.vue组件代码:

  1. <template>
  2. <div>
  3. <a-input :value="inputValue" @input="handlerInput"></a-input>
  4. <p>{{ inputValue }} -> lastLetter is {{ inputValueLastLetter }}</p>
  5. <p>appName: {{ appName }}, appNameWithVersion : {{ appNameWithVersion }}</p>
  6. <p>userName : {{ userName }}, firstLetter is : {{ firstLetter }}</p>
  7. <button @click="handleChangeAppName">修改appName和user.js中的userName</button>
  8. <p>动态给state增加appVersion: {{ appVersion }}</p>
  9. <button @click="handleActionChangeAppName">通过Action修改appName</button>
  10. <button @click="registerModule">动态注册模块</button>
  11. <p v-for="(li, index) in todoList" :key="index">{{ li }}</p>
  12. </div>
  13. </template>
  14. <script>
  15. import AInput from "_c/AInput.vue";
  16. import AShow from "_c/AShow.vue";
  17. //变量的解构赋值
  18. import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
  19. import { stat } from "fs";
  20. export default {
  21. name: "store",
  22. data() {
  23. return {
  24. inputValue: ""
  25. };
  26. },
  27. components: {
  28. AInput: AInput,
  29. AShow: AShow
  30. },
  31. computed: {
  32. //ES6展开操作符 mapState展开会形成一个对象 使用对象展开运算符将此对象混入到外部对象中
  33. ...mapState({
  34. appName: state => state.appName,
  35. appVersion: state => state.appVersion,
  36. userName: state => state.user.userName,
  37. todoList: state => (state.user.todo ? state.user.todo.todoList : [])
  38. }),
  39. // 使用对象展开运算符将 getter 混入 computed 对象中
  40. // ...mapGetters(["appNameWithVersion"]),
  41. appNameWithVersion() {
  42. //通过属性访问getters,Getter 会暴露为 store.getters 对象,可以以属性的形式访问这些值:
  43. return this.$store.getters.appNameWithVersion;
  44. },
  45. ...mapGetters(["firstLetter"]),
  46. inputValueLastLetter() {
  47. return this.inputValue.substr(-1, 1);
  48. }
  49. },
  50. methods: {
  51. handlerInput(val) {
  52. this.inputValue = val;
  53. },
  54. //
  55. ...mapMutations([
  56. "SET_USER_NAME", //将 `this.SET_USER_NAME()` 映射为 `this.$store.commit('SET_USER_NAME')`
  57. "SET_APP_NAME" //将 `this.SET_APP_NAME()` 映射为 `this.$store.commit('SET_APP_NAME')`
  58. ]),
  59. ...mapActions([
  60. "updateAppName" //将 `this.updateAppName()` 映射为 `this.$store.dispatch('updateAppName')`
  61. ]),
  62. handleChangeAppName() {
  63. this.SET_APP_NAME({
  64. appName: "newAppName"
  65. });
  66. this.SET_USER_NAME({
  67. userName: "shuyujie"
  68. });
  69. this.$store.commit("SET_APP_VERSION");
  70. },
  71. handleActionChangeAppName() {
  72. //第一种调用Action的方法
  73. //this.$store.dispatch('updateAppName')
  74. //第二种调用Action的方法
  75. this.updateAppName();
  76. },
  77. registerModule() {
  78. this.$store.registerModule(["user", "todo"], {
  79. state: {
  80. todoList: ["学习mutations", "学习actions"]
  81. }
  82. });
  83. }
  84. }
  85. };
  86. </script>

效果图:

此外需要注意的是,若模块使用了命名空间(namespaced:true),则访问模块中定义的模块时候需要采用这种方式:

  1. ...mapMutations("user/xxx",[
  2. "SET_USER_NAME", //将 `this.SET_USER_NAME()` 映射为 `this.$store.commit('SET_USER_NAME')`
  3. "SET_APP_NAME" //将 `this.SET_APP_NAME()` 映射为 `this.$store.commit('SET_APP_NAME')`
  4. ])

Vuex基础-Module的更多相关文章

  1. Vuex 基础

    其他章节请看: vue 快速入门 系列 Vuex 基础 Vuex 是 Vue.js 官方的状态管理器 在vue 的基础应用(上)一文中,我们已知道父子之间通信可以使用 props 和 $emit,而非 ...

  2. vuex中module的命名空间概念

    vuex中module的命名空间概念 默认情况下,模块内部的 action.mutation 和 getter 是注册在全局命名空间的. 弊端1:不同模块中有相同命名的mutations.action ...

  3. vuex的module的简单实用方法

    当我们的项目越来越大的时候,我们就开始使用vuex来管理我们的项目的状态.但是如果vuex的状态多了呢,这个时候module就登场了.看了一下官方的文档,很详细,但是没有demo让初学者很头疼.那我就 ...

  4. vuex 基础:教程和说明

    作者注:[2016.11 更新]这篇文章是基于一个非常旧的 vuex api 版本而写的,代码来自于2015年12月.但是,它仍能针对下面几个问题深入探讨: vuex 为什么重要 vuex 如何工作 ...

  5. Vuex基础-Mutation

    借助官网的一张图,更改 Vuex 的 store 中的状态的唯一方法是提交 mutation.不可以直接对其进行赋值改变.需要注意的是,mutations只能做一些同步的操作. ​​​ 代码结构: ​ ...

  6. Vuex基础-Getter

    官方地址:https://vuex.vuejs.org/zh/guide/getters.html Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性).就像 ...

  7. Vuex基础-State

    官方地址:https://vuex.vuejs.org/zh/guide/state.html 由于 Vuex 的状态存储是响应式的,从 store 实例中读取状态最简单的方法就是在计算属性中返回某个 ...

  8. [Vuex系列] - Module的用法(终篇)

    于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿.为了解决以上问题,Vuex 允许我们将 store 分割成模块(module).每 ...

  9. vuex之module的使用

    一.module的作用 由于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿. 为了解决以上问题,Vuex 允许我们将 store 分 ...

随机推荐

  1. 微信开发准备(二)--springmvc+mybatis项目结构的搭建

    转自:http://www.cuiyongzhi.com/post/34.html 前面一篇有说道如何在MyEclipse中搭建maven项目,这里将继续介绍如何在搭建好的基础maven项目中引入我们 ...

  2. Django 学习之---静态文件处理详解

    前言: 1.静态文件是指 网站中的 js, css, 图片,视频等文件 2.静态文件放在对应的 app 下的 static 文件夹中 或者 STATICFILES_DIRS 中的文件夹中. 当 DEB ...

  3. DAY11-MYSQL单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...

  4. springboot中单元测试

    测试service: 测试api:

  5. WCF客户端调用并行最大同时只支持两个请求

    做项目的时候发现 频繁调用WCF服务时 明明一次性发起了几十个请求 而在服务端记录的日志却显示出现了排队的迹象 并且都是最大并发数为2 在网上狂搜 大家给出来的解决方法都是增加web.config里面 ...

  6. ngx-bootstrap使用01 安装ngx-bootstrap和bootstrap及其使用、外部样式引入

    1 版本说明 2 新建一个angular项目 ng new 项目名 --stayle=scss 代码解释:创建一个样式文件格式为SCSS的angular项目 技巧01:由于我angular-cli的版 ...

  7. MyBatis02 MyBatis基础知识之Mapper映射器

    1 Mapper映射器是什么 是符合映射文件要求的接口 接口要求 a. 方法名要与sql的id一致. b. 方法的参数类型要与parameterType一致. c. 方法的返回类型要与resultTy ...

  8. Win10_禁用自动更新(官方版)

    1> win键>输入服务>打开>找到windowsUpdate-->启动类型为-禁用 -->> 恢复失三个选项改为-->>无操作 2>win ...

  9. Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫

    如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:P ...

  10. mongodb3.0版本的2种引擎对比

    mongodb3.0以后 增加了wiredtiger引擎.常规引擎也升级到MMAPv1引擎(MongoDB2.6及以下版本用的是MMAP引擎):   mmapv1引擎:             col ...