1、vuex 动态模块配置

import Vue from 'vue'
import Vuex from 'vuex'
import store from '@/store'; // 使用Vuex插件,即使插件被调用多次,插件也只会安装一次
Vue.use(Vuex); // state存储数据的状态
const state = { // 数据状态
name: 'mfg'
} // getters获取数据状态 const getters = { // 可以使用store.getters.myName获取数据
myName: state => {
return state.name
}
} // mutations更改数据状态的唯一方法
const mutations = { // 每个mutations都有一个事件类型type和回调函数。下面代码type为editName,回调函数即完成数据更改。
// agr为参数
editName(state, arg) {
state.name = arg;
}
} // actions提交的是mutation,处理的异步操作
const actions = { // 传递的参数为arg
editNameAction({ commit, state }, arg) {
commit('editName', arg)
}
} // registerModule,在 store 创建之后,注册模块
// 模块动态注册功能可以让其他vue插件使用注册好的新模块
store.registerModule('myNameSpace', { // 命名空间,模块具有更高的封装度和复用性
namespaced: true,
state,
mutations,
getters,
actions
})

或者组件注册:

<script>
import storeIndex from '../protect/store'
import store from '@/store' export default {
name: 'intelligence',
beforeMount() {
//组件注册store的命名空间
store.registerModule('intelligence', storeIndex)
},
destroyed() {
//组件销毁store的命名空间
store.unregisterModule('intelligence')
}
}
</script> /protect/store文件:
export default {
namespaced: true,
modules: {
common,
workflow,
configfile,
sysdetail,
unitdetail
}
}

2、vue单文件demo

<template>
<div>
<!-- 使用mapState获取数据状态 -->
<p>{{name}}</p>
<!-- 使用mapGetters获取数据状态 -->
<p>{{myName}}</p>
<!-- 使用mapMutations更改数据状态 -->
<el-button @click="edit('abc')">修改名字</el-button>
<!-- 使用mapActions更改数据状态 -->
<el-button @click="edit2('def')">修改名字2</el-button>
</div>
</template>
<script> import sti from 'commons/sti';
import './store'; // 辅助函数mapMutations, mapActions, mapGetters, mapState
import { mapMutations, mapActions, mapGetters, mapState } from 'vuex'; export default sti.page({
computed: { // 使用对象展开运算符将此对象混入到外部对象中
// 第一个参数为模块上下文myNameSpace
...mapState('myNameSpace', {
name: state => state.name
}), // 使用对象展开运算符将此对象混入到外部对象中
// 第一个参数为模块上下文myNameSpace
...mapGetters('myNameSpace', ['myName'])
}, data() {
return {}
}, methods: { // 第一个参数为模块上下文myNameSpace
...mapMutations('myNameSpace', ['editName']), // 第一个参数为模块上下文myNameSpace ...mapActions('myNameSpace', ['editNameAction']), // 也可以这样写
// ...mapActions(['myNameSpace/editNameAction']),
edit(arg) { // 更新数据状态
this.editName(arg);
},
edit2(arg) { // 更新数据状态
this.editNameAction(arg);
}
}, mounted() {}
});
</script>

在mutations中可以将type设置为常量

const mutations = {

  [types.THEME_UPDATE](state, theme) {
state.appTheme = theme
}
}
const actions = {

  updateTheme: ({commit}, theme) => {
commit(types.THEME_UPDATE, theme)
}
}

3、严格模式

const store = new Vuex.Store({
// ...
strict: process.env.NODE_ENV !== 'production'
})

在严格模式下,无论何时发生了状态变更且不是由 mutation 函数引起的,将会抛出错误。这能保证所有的状态变更都能被调试工具跟踪到。

vue vuex 大型项目demo示例的更多相关文章

  1. vuejs学习——vue+vuex+vue-router项目搭建(三)

    前言 vuejs学习——vue+vuex+vue-router项目搭建(一) vuejs学习——vue+vuex+vue-router项目搭建(二) 为什么用vuex:组件之间的作用域独立,而组件之间 ...

  2. vuejs学习——vue+vuex+vue-router项目搭建(二)

    前言 最近比较忙,所有第二章发布晚了,不好意思各位. vuejs学习——vue+vuex+vue-router项目搭建(一) 中我们搭建好了vue项目,我相信大家已经体验了vue其中的奥妙了,接下来我 ...

  3. Vue/Egg大型项目开发(一)搭建项目

    项目Github地址:前端(https://github.com/14glwu/stuer)后端(https://github.com/14glwu/stuer-server) 项目线上预览:http ...

  4. vuejs学习——vue+vuex+vue-router项目搭建(一)

    前言 快年底了却有新公司邀请了我,所以打算把上家公司的学到一下技术做一些总结和分享. 现在vuejs都2.0了,我相信也有很多朋友和我一样实际项目还是选择vue1.0的或者给新手一些参考,不管在选择哪 ...

  5. 【vue】MongoDB+Nodejs+express+Vue后台管理项目Demo

    ¶项目分析 一个完整的网站服务架构,包括:   1.web frame ---这里应用express框架   2.web server ---这里应用nodejs   3.Database ---这里 ...

  6. 后端狗的Vue学习历程(一) - demo示例与基本逻辑语法

    目录 demo的三部分结构 判断:v-if.v-else-if.v-else 循环:v-for 事件绑定 v-on:eventType 内容输入的双向绑定v-model 源码:Github demo的 ...

  7. Vue/Egg大型项目开发(二)数据库设计

    项目Github地址:前端(https://github.com/14glwu/stuer)后端(https://github.com/14glwu/stuer-server) 项目线上预览:http ...

  8. 【分享】Vue 资源典藏(UI组件、开发框架、服务端、辅助工具、应用实例、Demo示例)

    Vue 资源典藏,包括:UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和 ...

  9. Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例

    Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和WeUI的组件库 ...

随机推荐

  1. python中join函数的用法

    这个函数可以对字符串按照某种方式进行拼接,比如你要在三个字母中间都添加一个特定字符,就可以用这个函数实现 result = '*'.join(['A','B','C']) print(result) ...

  2. 洛谷——P2799 国王的魔镜

    P2799 国王的魔镜 题目描述 国王有一个魔镜,可以把任何接触镜面的东西变成原来的两倍——只是,因为是镜子嘛,增加的那部分是反的.比如一条项链,我们用AB来表示,不同的字母表示不同颜色的珍珠.如果把 ...

  3. Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)

    Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

  4. Spring的远程调用

    Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易 四种远程调用技术: ◆ 远程方法调用(RMI) ◆ Caucho的Hessian和Burlap ...

  5. BZOJ 2818 Gcd(莫比乌斯反演)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2818 [题目大意] 给定整数N,求1<=x,y<=N且Gcd(x,y)为素 ...

  6. [xsy2880]取石子游戏

    题意:有$n$堆石子,每堆石子数量相同,以质因数分解给出,不停地从$1$到$n$依次拿石子,使得取完后石子个数为原来的因数(不能不取),当一堆只剩$1$个时结束,问在每堆石子结束的方案数 记石子个数为 ...

  7. mac 安装 composer

    使用 curl 指令下载: curl -sS https://getcomposer.org/installer | php 或是沒有安裝 curl ,也可以用 php 指令下载: php -r &q ...

  8. 翻译:Spring-Framework-Reference Document:11-Transaction Management

    TUESDAY, 07 APRIL Comprehensive transaction support is the most compelling reasons to use the Spring ...

  9. Android疑难杂症之KillProcess 和System.exit 无效

    以下所讲,浓缩在 https://github.com/wytings/CrashDemo 首先就这个名字来说,kill了process 或者 system.exit确实已经把APP杀掉了,特别是当你 ...

  10. javat Itext实践 pdf

    1.简介 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转化为PDF文件. 下载地址:https:/ ...