vue vuex 大型项目demo示例
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示例的更多相关文章
- vuejs学习——vue+vuex+vue-router项目搭建(三)
前言 vuejs学习——vue+vuex+vue-router项目搭建(一) vuejs学习——vue+vuex+vue-router项目搭建(二) 为什么用vuex:组件之间的作用域独立,而组件之间 ...
- vuejs学习——vue+vuex+vue-router项目搭建(二)
前言 最近比较忙,所有第二章发布晚了,不好意思各位. vuejs学习——vue+vuex+vue-router项目搭建(一) 中我们搭建好了vue项目,我相信大家已经体验了vue其中的奥妙了,接下来我 ...
- Vue/Egg大型项目开发(一)搭建项目
项目Github地址:前端(https://github.com/14glwu/stuer)后端(https://github.com/14glwu/stuer-server) 项目线上预览:http ...
- vuejs学习——vue+vuex+vue-router项目搭建(一)
前言 快年底了却有新公司邀请了我,所以打算把上家公司的学到一下技术做一些总结和分享. 现在vuejs都2.0了,我相信也有很多朋友和我一样实际项目还是选择vue1.0的或者给新手一些参考,不管在选择哪 ...
- 【vue】MongoDB+Nodejs+express+Vue后台管理项目Demo
¶项目分析 一个完整的网站服务架构,包括: 1.web frame ---这里应用express框架 2.web server ---这里应用nodejs 3.Database ---这里 ...
- 后端狗的Vue学习历程(一) - demo示例与基本逻辑语法
目录 demo的三部分结构 判断:v-if.v-else-if.v-else 循环:v-for 事件绑定 v-on:eventType 内容输入的双向绑定v-model 源码:Github demo的 ...
- Vue/Egg大型项目开发(二)数据库设计
项目Github地址:前端(https://github.com/14glwu/stuer)后端(https://github.com/14glwu/stuer-server) 项目线上预览:http ...
- 【分享】Vue 资源典藏(UI组件、开发框架、服务端、辅助工具、应用实例、Demo示例)
Vue 资源典藏,包括:UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和 ...
- Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例
Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和WeUI的组件库 ...
随机推荐
- Check if a user is in a group
groups or groups user
- luogu P1002 过河卒
题目描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒”. ...
- 【c++随手记】编程基础之输入输出
今天试了下noi oj的1.1节,随便总结一下. [cout左对齐右对齐的方法] #include<iostream> #include<cstdio> #include< ...
- ElasticSearch安装为Windows服务
目前我都是在windows的环境下操作是Elasticsearch,并且喜欢使用命令行 启动时通过cmd直接在elasticsearch的bin目录下执行elasticsearch 这样直接启动的话集 ...
- Ubuntu 16.04搭建OpenVPN服务器以及客户端的使用
说明:启动时注意用户权限,比如root用户启动. Ubuntu: 服务器环境:Ubuntu 16.04 64位系统 内网IP:10.143.80.116 外网IP:203.195.1.2 OpenVP ...
- httpWebRequest 文件下载
服务版本: go file system ssdb github: https://github.com/dtxlink/gfs 上一篇: 一个 go 文件服务器 ssdb 通过 httpWebReq ...
- java通过UUID生成16位唯一订单号
import java.util.UUID; public class T { public static String getOrderIdByUUId() { int machineId = 1; ...
- UITabBarController 详解
// UITabBarController 标签视图控制 // 主要管理没有层级关系的视图控制器 // 1. ViewControllers 所有被管理的视图控制器, 都在这个数组中 // 2. se ...
- PTCSolution 关注
2013.12.28更新: 经过几次整理和再次租赁下线,点击数明显增加,现在995下线点击数3413.再将2页半的点击数低的下线替换掉,AVG肯定超过4. 这么短的时间整理效果如此明显是我没有想到 ...
- openresty记录响应body乱码问题
问题背景 最近新上了一个功能,openresty通过syslog记录请求日志,然后由logstash推送至ES.测试上线时未发现这个问题,在日常查看日志的过程中,发现logstash推送有错误日志,错 ...