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 ...
随机推荐
- CSP-J2019游记&解题报告
考前一天晚上失眠.......(其实主要不是因为考试的原因) 很幸运,我们学校就是一个考点,本场作战,应该有一点加持吧. 上午在家复习,看到一篇关于PN532模拟小米手环加密卡的文章,于是,,,,,, ...
- ESP8266低功耗解决的其中一个问题(芯片发热,影响旁边的温湿度芯片)
这个项目的这个问题困扰了自己好长时间了,ESP8266芯片发热,导致了旁边的温湿度传感器采集不了空气中的温度....采集的温度是芯片发热的温度 一直采集出来的是30多度......尽管空气温度10几度 ...
- [RN] React Native 查看console打印出来的内容
我们在调试React Native 程序的时候,经常会用到Js的打印语句Console.log等 但我们一脸蒙逼的时候,启动的时候完全看不到打印的内容在哪儿呢??? 原来还要在命令行下输入一个语句才能 ...
- 记一次CDH集群日志数据清理
背景 集群运行一段时间(大概一月多)后,cloudera manager管理界面出现爆红,爆红的组件有hdfs.zookeeper. 发现问题 点击详细内容查看,报日志空间不够的错误.初步判断是各个组 ...
- kube-promethues监控告警详解(邮件、钉钉、微信、自研平台)
Alertmanager已经在前面Prometheus初体验(三)已经介绍过了.现在介绍一下在kube-promethues里面怎么修改alertmanager配置文件,以及怎么通过各种媒介发送信息. ...
- MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法
MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法 事情的起因 今天课堂上要展示小组项目,需要用一个软件叫W ...
- 【Qt开发】vs2017+qt5.x编译32位应用
概述 最近有同学私信我,问如何使用vs2017+qt5.10编译出32位的应用,需要使用msvc2017_x86的插件,然而qt官网并没有提供,只能使用源码编译生成msvc2017_x86插件,使用n ...
- Spring Boot-intellij idea导入方式搭建SpringBoot
Spring Boot概念 从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用.简便起见,该框架也提供了命令行界面,它可以用来运行和测试Boot应用.框架的发布版 ...
- web编辑器的使用比较
一开始测试的时候发现都还可以,只是出于后续考虑,选择了ckeditor,但是有时候会莫名出现Cannot read property 'XXX' of undefined,如下: Uncaught T ...
- Windows10环境下 Nginx+ffmpeg自搭服务器制作RTMP直播流
Windows10环境下 Nginx+ffmpeg自搭服务器制作RTMP直播流学习笔记 所需条件: nginx-rtmp-module(带rtmp模块) ,链接:https://link.jiansh ...