Vuex模块:不开启命名空间
模块不开启命名空间时,会共享全局命名空间。
{
state: {
模块1: "局部状态1",
模块2: "局部状态2"
},
getters: {
getter1() {},
getter2() {}
},
mutations: ["局部变化1", "局部变化2"],
actions: ["局部动作1", "局部动作2"]
}
mapState、mapGetters、mapMutations、mapActions参数是数组。
mapXXXs(['属性名1','属性名2'])
一 项目结构

二 main.js
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store/index"; Vue.config.productionTip = false; new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
三 index.js
import Vue from "vue";
import Vuex from "vuex";
import cat from "./modules/cat";
import dog from "./modules/dog"; Vue.use(Vuex); export default new Vuex.Store({
modules: { cat, dog }
});
四 cat.js
export default {
// 局部状态
state: {
name: "蓝白英短",
age: 1
},
// 局部读取
getters: {
catDesc: state => "宠物:" + state.name
},
// 局部变化
mutations: {
increment(state, payload) {
state.age += payload.num;
}
},
// 局部动作
actions: {
grow(context, payload) {
setTimeout(() => {
context.commit("increment", payload);
}, 1000);
}
}
};
五 dog.js
export default {
// 局部状态
state: {
name: "拉布拉多",
age: 1
},
// 局部读取
getters: {
dogDesc: state => "宠物:" + state.name
},
// 局部变化
mutations: {
increment(state, payload) {
state.age += payload.num;
}
},
// 局部动作
actions: {
grow(context, payload) {
setTimeout(() => {
context.commit("increment", payload);
}, 1000);
}
}
};
六 HelloWorld.vue
<template>
<div class="hello">
<h3>Vuex状态树</h3>
<div>{{this.$store.state}}</div>
<h3>mapState</h3>
<div>{{cat}}</div>
<div>{{dog}}</div>
<h3>mapGetters</h3>
<div>{{catDesc}}</div>
<div>{{dogDesc}}</div>
<h3>mapMutations</h3>
<!-- 猫狗的年龄都变化 -->
<button @click="increment({num:1})">变化</button>
<h3>mapActions</h3>
<!-- 猫狗的年龄都变化 -->
<button @click="grow({num:1})">动作</button>
</div>
</template> <script>
import { mapState, mapGetters, mapMutations, mapActions } from "vuex"; export default {
name: "HelloWorld",
computed: {
...mapState(["cat", "dog"]),
...mapGetters(["catDesc", "dogDesc"])
},
methods: {
...mapMutations(["increment"]),
...mapActions(["grow"])
}
};
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
</style>
七 运行效果

Vuex模块:不开启命名空间的更多相关文章
- Vuex模块:开启命名空间
模块开启命名空间后,享有独自的命名空间. { "模块1":{ state:{}, getters:{}, mutations:{}, actions:{} }, "模块2 ...
- TypeScript模块系统、命名空间、声明合并
命名空间 命名空间能有效避免全局污染.在ES6引入模块之后,命名空间就较少被提及了.如果使用了全局的类库,命名空间仍是一个好的解决方案. namespace Shape{ const pi = Mat ...
- 在vue组件中访问vuex模块中的getters/action/state
store的结构: city模块: 在各模块使用了命名空间的情况下,即 namespaced: true 时: 组件中访问模块里的state 传统方法: this.$store.state['模块名' ...
- 优雅的写好Vue项目代码 — 路由拆分、Vuex模块拆分、element按需加载
目录 路由的拆分 VUEX模块拆分 Element UI库按需加载的优雅写法 路由的拆分 项目较大路由较多时,路由拆分是一个不错的代码优化方案,按不同业务分为多个模块,结构清晰便于统一管理. requ ...
- vuex 模块
今天,在我编写系统中一个模块功能的时候,由于我使用vuex存储数据的状态,并分模块存储.我是这样在存储文件中定义state,getters,actions,mutations的,我打算在不同模块文件都 ...
- day10(闭包、import模块、函数命名空间)
#闭包:嵌套函数,内部函数调用外部函数的变量 # def outer(): # a = 1 # def inner(): # print(a) # inner() # outer() def oute ...
- Python之路(第四十一篇)线程概念、线程背景、线程特点、threading模块、开启线程的方式
一.线程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是 ...
- python用WMI模块获取系统命名空间
可以和winmgmts的查询页面对应 from win32com.client import GetObject import pywintypes result=[] def enum_namesp ...
- vuex模块相互调用
https://segmentfault.com/a/1190000009434398
随机推荐
- ASP.NET Web API 使用Swagger
ASP.NET Web API 使用Swagger使用笔记 最近换了工作,其中Webapi这块没有文档,之前有了解过Swagger借此机会好好整理下常用的地方分享给有需要的小伙伴. 概述: 1.s ...
- 不启动或进入虚拟机,查看 KVM 虚拟机中的网卡信息
#!bin/bash#作者:liusingbon#功能:#脚本使用工具guestmount,可以将虚拟机的磁盘系统挂载到真实机文件系统中#Centos7.2中安装libguestfs-tools-c, ...
- C#基础知识之System.AppDomain类
进程是存在独立的内存和资源的,但是AppDomain仅仅是逻辑上的一种抽象.一个process可以存在多个AppDomain.各个AppDomain之间的数据时相互独立的.一个线程可以穿梭多个AppD ...
- ztree点击加号+触发ajax请求
之前做的时候一直是点击节点才触发ajax事件,配置如下:发现点击节点前面的“+”没有反应,后来发现,应该添加一个折叠的事件. onExpand:zTreeOnClick事件和onClick的一样. v ...
- overflow:hidden失效问题
2018-08-03 Questions about work 这几天开发的时候遇到了个问题,如图1. 写了个demo demo 地址 由于页面并没有进行整体缩放,导致在小屏幕手机上显示会有异常.PM ...
- Spring Boot关于layui的通用返回类
1.关于layui的通用返回类 code.count.data.msg public class Msg { private long code = 0; private long count = 0 ...
- java 生成透明背景图片
//开始绘图 graphics2d.setBackground(Color.WHITE); graphics2d.clearRect(0, 0, width, height); graphics2d. ...
- POI拆分单元格,并设置拆分后第一个cell的值为空cell的值
// 从第A7开始,拆分单元格 CellReference ref = new CellReference("A7"); //遍历sheet中的所有的合并区域 for (int i ...
- LeetCode--059--螺旋矩阵 II(python)
效率超级低,但是能过.... class Solution: def generateMatrix(self, n): tR = tC = 0 dR = n-1 dC = n-1 x = [[0 fo ...
- postman-鉴权
概念 Cookie和鉴权的区别,cookie一般指缓存在本地的数据:鉴权一般指验证用户是否拥有访问系统的权利 鉴权分类 Basic auth:基础鉴权,数据没有加密可明文显示,一般在测试环境使用,不在 ...