vuex 的基本使用
工程目录

主要关注store 文件夹下的文件
store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
// import getters from './store/getters.js'
// import actions from './store/actions.js'
// import mutations from './store/mutations.js'
import types from './types' // children module
import users from './modules/users.js' Vue.use(Vuex) const state = {
count: 1
} const mutations = {
[types.INCREMENT]: (state, n) => {
state.count = state.count + n
},
[types.DECREMENT]: (state, n) => {
state.count = state.count - n
}
} const actions = {
increment: (context, n = 1) => {
context.commit(types.INCREMENT, n)
},
decrement: (context, commit, n = 1) => {
context.commit(types.DECREMENT, n)
}
} export default new Vuex.Store({
state,
mutations,
actions,
modules: {
users
}
})
store/modules/users.js
import types from '../types'
const state = {
username: 'xiaojf'
}
const mutations = {
[types.CHANGEUSERNAME]: (state, username) => {
state.username = username
}
}
const actions = {
changeUsername (context, username = 'zhangsan') {
context.commit(types.CHANGEUSERNAME, username)
}
}
export default {
state,
mutations,
actions
}
/components/test.vue
<template>
<div>
<div class="test">{{name}}</div>
<div class="test">{{this.$store.state.count}}</div>
<button v-on:click="increment()">increment</button>
<button v-on:click="decrement()">decrement</button> <br>
this is users module state <span style="color: red;" v-on:click="changeUsername()"> {{this.$store.state.users.username}}</span> <br>
<test2></test2>
</div>
</template> <script>
import test2 from './test2' export default {
name: 'test',
data: function () {
return {
name: 'xiaojf'
}
},
components: {
test2
},
methods: {
increment () {
// mutation
this.$store.dispatch('increment', 1)
},
decrement () {
// action
this.$store.dispatch('decrement', 2)
},
changeUsername () {
// children module's action
this.$store.dispatch('changeUsername', 'xiaojianfeng')
}
}
}
</script> <style scoped>
.test {
font-size: 28px;
color: red;
}
</style>
/components/test.vue
<template>
<div>
this is test2 {{this.$store.state.count}}
</div>
</template> <script>
export default {
name: 'test2',
data: function () {
return {
name: 'xiaojf'
}
}
}
</script>
vuex 的基本使用的更多相关文章
- 关于Vue.js 2.0 的 Vuex 2.0,你需要更新的知识库
应用结构 实际上,Vuex 在怎么组织你的代码结构上面没有任何限制,相反,它强制规定了一系列高级的原则: 应用级的状态集中放在 store 中. 改变状态的唯一方式是提交mutations,这是个同步 ...
- vuex复习方案
这次复习vuex,发现官方vuex2.0的文档写得太简略了,有些看不懂了.然后看了看1.0的文档,感觉很不错.那以后需要复习的话,还是先看1.0的文档吧.
- vuex 初体验
vuex是vue的状态管理工具,vue进阶从es6和npm开始,es6推荐阮一峰大神的教程. vuex学习从官方文档和一个记忆小游戏开始.本着兴趣为先的原则,我先去试玩了一把-->. Vuex ...
- vuex(1.0版本写法)
Vuex 是一个专门为 Vue.js 应用所设计的集中式状态管理架构. 官方文档:http://vuex.vuejs.org/zh-cn/ 2.0和1.0都能在此找到 每一个 Vuex 应用的核心就 ...
- 关于Vue vuex vux 文档
01. vue 链接 http://vuejs.org.cn/guide/ 02. vuex ----->>状态管理模块儿<<------- https://vuex.vue ...
- vuex
英文:(Introduction)中文:https://github.com/vuejs/vuex/issues/176(贡献者努力中)
- Vue 2.0 + Vue Router + Vuex
用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...
- Vue2.X的状态管理vuex记录
记住上述的顺序情况:想要改变state,只能通过Mutation,虽然action可以直接改变state,这样会使每个状态可以方便的跟踪和记录(用Devtools跟踪) vue Method -- ...
- 在vue1.0遇到vuex和v-model的坑
事情是这样的,在开发项目的过程中我使用了vuex并且在store中定义了一个保存用户信息的对象 userInfo : { 'nickName' : '', // 昵称 'password' :'', ...
- vuex 笔记
Vuex 笔记 一个简单的状态管理 单一数据源: const sourceOfTruth = {} const vmA = new Vue({ data: sourceOfTruth }) const ...
随机推荐
- Failed to configure a DataSource: 'url' attribute is not specified and no embe...
问题分析及解决方案 问题原因: Mybatis没有找到合适的加载类,其实是大部分spring - datasource - url没有加载成功,分析原因如下所示. DataSourceAutoConf ...
- Zabbix4.0国内下载源
国内zabbix源总结 目前发现的有以下几个站点: 1.阿里巴巴开源镜像站(推荐使用) 地址:https://mirrors.aliyun.com/zabbix/ 2.华为开源镜像站(推荐使用) 地址 ...
- Mycat 全局系列号(转载)
概述 本篇文章介绍mycat怎样在分库分表的情况下保证主键的全局唯一方法,接下来就来分析三种方法各自的优缺点. 配置 文件方式获取 1.修改server配置文件 vim server.xml < ...
- Python 08 skimage
原文:https://www.cnblogs.com/xdjun/p/7874794.html 命令: pip install numpy pip install scipy pip install ...
- PHP生成随机数;订单号唯一
//8-12位随机数 function makeRand($num=){ $strand = (; if(strlen($strand)<$num){ $strand = str_pad($st ...
- Day17:web前端开发面试题
1.JavaScript 数据类型有哪些? JavaScript 变量能够保存多种数据类型:数值.字符串值.数组.对象等等: var length = 7; // 数字 var lastName = ...
- 设计模式之MVC和MVT
MVC各部分的功能 全拼为Model-View-Controller(如上图所示) M(模型)全拼为Model,主要封装对数据库层的访问(内嵌ORM框架),对数据库中的数据进行增.删.改.查操作. V ...
- Noip2019暑期训练2 反思
经过两次测试,通过深刻的反思,我主要发现了以下易犯错误: 1.做题目时过于追求速度,导致好几处代码都出现手误打错的现象!而且,千万不要图快.图方便就复制粘贴,非常容易出错!(例如T3-party中直接 ...
- Django基础之redirect()
参数可以是: 一个模型: 将调用模型的get_absolute_url()函数 一个视图, 可以带有函数: 可以使用urlresolvers.reverse来反向解析名称 一个绝对的或相对的URL, ...
- FormData实现文件上传
我用到FormData传输的使用场景:vant UI组件里面 的图片上传这块,需要调用后台的图片上传接口,使用的是FormData方式上传的 https://www.cnblogs.com/hutuz ...