Vuex基础-Module
官方API地址:https://vuex.vuejs.org/zh/guide/modules.html
前面几节课写的user.js就称为一个module,这样做的原因是:由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。每个模块又是独立的store,拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割。
下面以在根store对象动态注册模块为例来对module做演示:
store.vue代码:
- <template>
- <div>
- <a-input :value="inputValue" @input="handlerInput"></a-input>
- <p>{{ inputValue }} -> lastLetter is {{ inputValueLastLetter }}</p>
- <p>appName: {{ appName }}, appNameWithVersion : {{ appNameWithVersion }}</p>
- <p>userName : {{ userName }}, firstLetter is : {{ firstLetter }}</p>
- <button @click="handleChangeAppName">修改appName和user.js中的userName</button>
- <p>动态给state增加appVersion: {{ appVersion }}</p>
- <button @click="handleActionChangeAppName">通过Action修改appName</button>
- <button @click="registerModule">动态注册模块</button>
- <p v-for="(li, index) in todoList" :key="index">{{ li }}</p>
- </div>
- </template>
- <script>
- import AInput from "_c/AInput.vue";
- import AShow from "_c/AShow.vue";
- //变量的解构赋值
- import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
- import { stat } from "fs";
- export default {
- name: "store",
- data() {
- return {
- inputValue: ""
- };
- },
- components: {
- AInput: AInput,
- AShow: AShow
- },
- computed: {
- //ES6展开操作符 mapState展开会形成一个对象 使用对象展开运算符将此对象混入到外部对象中
- ...mapState({
- appName: state => state.appName,
- appVersion: state => state.appVersion,
- userName: state => state.user.userName,
- todoList: state => (state.todo ? state.todo.todoList : [])
- }),
- // 使用对象展开运算符将 getter 混入 computed 对象中
- // ...mapGetters(["appNameWithVersion"]),
- appNameWithVersion() {
- //通过属性访问getters,Getter 会暴露为 store.getters 对象,可以以属性的形式访问这些值:
- return this.$store.getters.appNameWithVersion;
- },
- ...mapGetters(["firstLetter"]),
- inputValueLastLetter() {
- return this.inputValue.substr(-1, 1);
- }
- },
- methods: {
- handlerInput(val) {
- this.inputValue = val;
- },
- //
- ...mapMutations([
- "SET_USER_NAME", //将 `this.SET_USER_NAME()` 映射为 `this.$store.commit('SET_USER_NAME')`
- "SET_APP_NAME" //将 `this.SET_APP_NAME()` 映射为 `this.$store.commit('SET_APP_NAME')`
- ]),
- ...mapActions([
- "updateAppName" //将 `this.updateAppName()` 映射为 `this.$store.dispatch('updateAppName')`
- ]),
- handleChangeAppName() {
- this.SET_APP_NAME({
- appName: "newAppName"
- });
- this.SET_USER_NAME({
- userName: "shuyujie"
- });
- this.$store.commit("SET_APP_VERSION");
- },
- handleActionChangeAppName() {
- //第一种调用Action的方法
- //this.$store.dispatch('updateAppName')
- //第二种调用Action的方法
- this.updateAppName();
- },
- registerModule() {
- this.$store.registerModule( "todo", {
- state: {
- todoList: ["学习mutations", "学习actions"]
- }
- });
- }
- }
- };
- </script>
效果图:
若给user动态一个模块,则store.vue组件代码:
- <template>
- <div>
- <a-input :value="inputValue" @input="handlerInput"></a-input>
- <p>{{ inputValue }} -> lastLetter is {{ inputValueLastLetter }}</p>
- <p>appName: {{ appName }}, appNameWithVersion : {{ appNameWithVersion }}</p>
- <p>userName : {{ userName }}, firstLetter is : {{ firstLetter }}</p>
- <button @click="handleChangeAppName">修改appName和user.js中的userName</button>
- <p>动态给state增加appVersion: {{ appVersion }}</p>
- <button @click="handleActionChangeAppName">通过Action修改appName</button>
- <button @click="registerModule">动态注册模块</button>
- <p v-for="(li, index) in todoList" :key="index">{{ li }}</p>
- </div>
- </template>
- <script>
- import AInput from "_c/AInput.vue";
- import AShow from "_c/AShow.vue";
- //变量的解构赋值
- import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
- import { stat } from "fs";
- export default {
- name: "store",
- data() {
- return {
- inputValue: ""
- };
- },
- components: {
- AInput: AInput,
- AShow: AShow
- },
- computed: {
- //ES6展开操作符 mapState展开会形成一个对象 使用对象展开运算符将此对象混入到外部对象中
- ...mapState({
- appName: state => state.appName,
- appVersion: state => state.appVersion,
- userName: state => state.user.userName,
- todoList: state => (state.user.todo ? state.user.todo.todoList : [])
- }),
- // 使用对象展开运算符将 getter 混入 computed 对象中
- // ...mapGetters(["appNameWithVersion"]),
- appNameWithVersion() {
- //通过属性访问getters,Getter 会暴露为 store.getters 对象,可以以属性的形式访问这些值:
- return this.$store.getters.appNameWithVersion;
- },
- ...mapGetters(["firstLetter"]),
- inputValueLastLetter() {
- return this.inputValue.substr(-1, 1);
- }
- },
- methods: {
- handlerInput(val) {
- this.inputValue = val;
- },
- //
- ...mapMutations([
- "SET_USER_NAME", //将 `this.SET_USER_NAME()` 映射为 `this.$store.commit('SET_USER_NAME')`
- "SET_APP_NAME" //将 `this.SET_APP_NAME()` 映射为 `this.$store.commit('SET_APP_NAME')`
- ]),
- ...mapActions([
- "updateAppName" //将 `this.updateAppName()` 映射为 `this.$store.dispatch('updateAppName')`
- ]),
- handleChangeAppName() {
- this.SET_APP_NAME({
- appName: "newAppName"
- });
- this.SET_USER_NAME({
- userName: "shuyujie"
- });
- this.$store.commit("SET_APP_VERSION");
- },
- handleActionChangeAppName() {
- //第一种调用Action的方法
- //this.$store.dispatch('updateAppName')
- //第二种调用Action的方法
- this.updateAppName();
- },
- registerModule() {
- this.$store.registerModule(["user", "todo"], {
- state: {
- todoList: ["学习mutations", "学习actions"]
- }
- });
- }
- }
- };
- </script>
效果图:
此外需要注意的是,若模块使用了命名空间(namespaced:true),则访问模块中定义的模块时候需要采用这种方式:
- ...mapMutations("user/xxx",[
- "SET_USER_NAME", //将 `this.SET_USER_NAME()` 映射为 `this.$store.commit('SET_USER_NAME')`
- "SET_APP_NAME" //将 `this.SET_APP_NAME()` 映射为 `this.$store.commit('SET_APP_NAME')`
- ])
Vuex基础-Module的更多相关文章
- Vuex 基础
其他章节请看: vue 快速入门 系列 Vuex 基础 Vuex 是 Vue.js 官方的状态管理器 在vue 的基础应用(上)一文中,我们已知道父子之间通信可以使用 props 和 $emit,而非 ...
- vuex中module的命名空间概念
vuex中module的命名空间概念 默认情况下,模块内部的 action.mutation 和 getter 是注册在全局命名空间的. 弊端1:不同模块中有相同命名的mutations.action ...
- vuex的module的简单实用方法
当我们的项目越来越大的时候,我们就开始使用vuex来管理我们的项目的状态.但是如果vuex的状态多了呢,这个时候module就登场了.看了一下官方的文档,很详细,但是没有demo让初学者很头疼.那我就 ...
- vuex 基础:教程和说明
作者注:[2016.11 更新]这篇文章是基于一个非常旧的 vuex api 版本而写的,代码来自于2015年12月.但是,它仍能针对下面几个问题深入探讨: vuex 为什么重要 vuex 如何工作 ...
- Vuex基础-Mutation
借助官网的一张图,更改 Vuex 的 store 中的状态的唯一方法是提交 mutation.不可以直接对其进行赋值改变.需要注意的是,mutations只能做一些同步的操作. 代码结构: ...
- Vuex基础-Getter
官方地址:https://vuex.vuejs.org/zh/guide/getters.html Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性).就像 ...
- Vuex基础-State
官方地址:https://vuex.vuejs.org/zh/guide/state.html 由于 Vuex 的状态存储是响应式的,从 store 实例中读取状态最简单的方法就是在计算属性中返回某个 ...
- [Vuex系列] - Module的用法(终篇)
于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿.为了解决以上问题,Vuex 允许我们将 store 分割成模块(module).每 ...
- vuex之module的使用
一.module的作用 由于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿. 为了解决以上问题,Vuex 允许我们将 store 分 ...
随机推荐
- 微信开发准备(二)--springmvc+mybatis项目结构的搭建
转自:http://www.cuiyongzhi.com/post/34.html 前面一篇有说道如何在MyEclipse中搭建maven项目,这里将继续介绍如何在搭建好的基础maven项目中引入我们 ...
- Django 学习之---静态文件处理详解
前言: 1.静态文件是指 网站中的 js, css, 图片,视频等文件 2.静态文件放在对应的 app 下的 static 文件夹中 或者 STATICFILES_DIRS 中的文件夹中. 当 DEB ...
- DAY11-MYSQL单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...
- springboot中单元测试
测试service: 测试api:
- WCF客户端调用并行最大同时只支持两个请求
做项目的时候发现 频繁调用WCF服务时 明明一次性发起了几十个请求 而在服务端记录的日志却显示出现了排队的迹象 并且都是最大并发数为2 在网上狂搜 大家给出来的解决方法都是增加web.config里面 ...
- ngx-bootstrap使用01 安装ngx-bootstrap和bootstrap及其使用、外部样式引入
1 版本说明 2 新建一个angular项目 ng new 项目名 --stayle=scss 代码解释:创建一个样式文件格式为SCSS的angular项目 技巧01:由于我angular-cli的版 ...
- MyBatis02 MyBatis基础知识之Mapper映射器
1 Mapper映射器是什么 是符合映射文件要求的接口 接口要求 a. 方法名要与sql的id一致. b. 方法的参数类型要与parameterType一致. c. 方法的返回类型要与resultTy ...
- Win10_禁用自动更新(官方版)
1> win键>输入服务>打开>找到windowsUpdate-->启动类型为-禁用 -->> 恢复失三个选项改为-->>无操作 2>win ...
- Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫
如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:P ...
- mongodb3.0版本的2种引擎对比
mongodb3.0以后 增加了wiredtiger引擎.常规引擎也升级到MMAPv1引擎(MongoDB2.6及以下版本用的是MMAP引擎): mmapv1引擎: col ...