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
随机推荐
- C# 中的DevExpress控件的使用
使用教程:http://training.evget.com/video/5110 C# System 程序集https://msdn.microsoft.com/zh-cn/library/mt4 ...
- linux 深入应用 NFS
以下实验大家用主机名来区分服务器端和客户端, 服务器端为 NFS_Server ip-192.168.1.4: 客户端为 NFS_Client ip-192.168.1.5: 实例一 将/tmp 分享 ...
- MAKEDEV - 建立设备
总览 (SYNOPSIS) cd dev; ./MAKEDEV -V cd dev; ./MAKEDEV [ -n ] [ -v ] update cd dev; ./MAKEDEV [ -n ] [ ...
- idea的使用技巧
* 简介:程序员每日都会花费数小时使用ide编写和调试代码,其中很多操作都是机械重复且频率非常高,本着"工欲善其事必先利其器"的精神,闷头写代码之外花点时间研究一下自己用的ide, ...
- libevent 网络库安装
./configure prefix=/tools/libevent make sudo make install
- bzoj5020 & loj2289 [THUWC 2017]在美妙的数学王国中畅游 LCT + 泰勒展开
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5020 https://loj.ac/problem/2289 题解 这个 appear 和 d ...
- 查看jar包内容
查看jar包内容 查看jar包内容的基本命令: jar tf jar-file 参数解释: The t option indicates that you want to view the table ...
- zip(), dict(), itertools.repeat(), list(迭代器)
*. zip(), dict() def demo_zip_dict(): keys = ['a', 'b', 'c'] values = [1, 2, 3] entrys = zip(keys, v ...
- RouterOS基础安装配置
安装 光盘安装 载入光盘,按"A"全选按"I"进行安装,然后提示不否用旧的配置,按"N"全新安装,警告时按"Y"确定 安 ...
- B/S超大文件上传与下载
最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...