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 ...
随机推荐
- janusgraph单机版安装
注:本次安装janusgraph基于es和hbse,所以先安装es和hbase 1.安装jdk 2.安装janusgraph 解压安装文件至/usr/janusgraph-0.3.1 unzip ja ...
- GIL锁是什么鬼?
参考链接: http://cenalulu.github.io/python/gil-in-python/ GIL不是Python特性 GIL是Python解释器(Cpython)时引入的概念,在JP ...
- 事件类型(onload)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- learning java FileVisitor 遍丽文件及路径
import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttribut ...
- CSS链接伪类:超链接的状态
一.状态: a:link{属性:值;} 链接默认状态 a:visited{属性:值;} 链接访问之后的状态 a:hover{属性:值;} 鼠标放到链接上显示的状态 a:active{属性:值;} 链接 ...
- 58、Spark Streaming: DStream的output操作以及foreachRDD详解
一.output操作 1.output操作 DStream中的所有计算,都是由output操作触发的,比如print().如果没有任何output操作,那么,压根儿就不会执行定义的计算逻辑. 此外,即 ...
- 关于 Mercury_Lc 说明
现在还主要在用 csdn 写博客,博客地址:https://blog.csdn.net/Mercury_Lc 这个是因为好奇,点了一下 一键搬家 ,就酱紫了. 主要更新,前往这个网址 https:// ...
- pycharm无法识别自己的文件夹的程序
网上找教程折腾了半天也没解决,然后我换了一下文件夹名称…… 文件夹名称不能用数字开头,否则识别不出来.
- SDN第五次上机实验
1.浏览RYU官网学习RYU控制器的安装和RYU开发入门教程,提交你对于教程代码的理解. 1.通过源码安装RYU控制器 sudo apt-get install python3-pip git clo ...
- $(window).load()方法的使用场景
一.$(window).load().window.onload=function(){}和$(document).ready()方法的区别 1.$(window).load() 和window.on ...