工程目录

主要关注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 的基本使用的更多相关文章

  1. 关于Vue.js 2.0 的 Vuex 2.0,你需要更新的知识库

    应用结构 实际上,Vuex 在怎么组织你的代码结构上面没有任何限制,相反,它强制规定了一系列高级的原则: 应用级的状态集中放在 store 中. 改变状态的唯一方式是提交mutations,这是个同步 ...

  2. vuex复习方案

    这次复习vuex,发现官方vuex2.0的文档写得太简略了,有些看不懂了.然后看了看1.0的文档,感觉很不错.那以后需要复习的话,还是先看1.0的文档吧.

  3. vuex 初体验

    vuex是vue的状态管理工具,vue进阶从es6和npm开始,es6推荐阮一峰大神的教程. vuex学习从官方文档和一个记忆小游戏开始.本着兴趣为先的原则,我先去试玩了一把-->. Vuex ...

  4. vuex(1.0版本写法)

    Vuex 是一个专门为 Vue.js 应用所设计的集中式状态管理架构. 官方文档:http://vuex.vuejs.org/zh-cn/  2.0和1.0都能在此找到 每一个 Vuex 应用的核心就 ...

  5. 关于Vue vuex vux 文档

    01. vue 链接 http://vuejs.org.cn/guide/ 02. vuex  ----->>状态管理模块儿<<------- https://vuex.vue ...

  6. vuex

    英文:(Introduction)中文:https://github.com/vuejs/vuex/issues/176(贡献者努力中)

  7. Vue 2.0 + Vue Router + Vuex

    用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...

  8. Vue2.X的状态管理vuex记录

    记住上述的顺序情况:想要改变state,只能通过Mutation,虽然action可以直接改变state,这样会使每个状态可以方便的跟踪和记录(用Devtools跟踪) vue Method   -- ...

  9. 在vue1.0遇到vuex和v-model的坑

    事情是这样的,在开发项目的过程中我使用了vuex并且在store中定义了一个保存用户信息的对象 userInfo : { 'nickName' : '', // 昵称 'password' :'', ...

  10. vuex 笔记

    Vuex 笔记 一个简单的状态管理 单一数据源: const sourceOfTruth = {} const vmA = new Vue({ data: sourceOfTruth }) const ...

随机推荐

  1. MyBatis框架的基本要素-核心接口和类的作用范围

    通过上面运行案例-查询用户表中的记录数. 非集成环境下的最佳作用域范围: SqlSessionFactoryBuilder 用过即丢,推荐作用域在方法体内. SqlSessionFactory 最佳作 ...

  2. FFT版题 [51 Nod 1028] 大数乘法

    题目链接:51 Nod 传送门 数的长度为10510^5105,乘起来后最大长度为2×1052\times10^52×105 由于FFT需要把长度开到222的次幂,所以不能只开到2×1052\time ...

  3. vigil deb 包制作

    前边有写过简单rpm 包的制作,现在制作一个简单的deb 包. deb 包的制作是通过源码编译+ fpm 环境准备 rust curl https://sh.rustup.rs -sSf | sh 配 ...

  4. tldr/cheat

    tldr 比man好用的查询命令查询工具, man很强大,但是 TLDR,too long dont read 安装 npm install -g tldr 使用说明 其他版本下载 https://g ...

  5. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  6. SQL基础-操纵表及插入、查询

    一.操纵表 1.表的关键信息 2.更新表名 更新表名:使用RENAME TABLE关键字.语法如下: RENAME TABLE 旧表名 TO 新表名; 比如,生产环境投产前备份teacher表,使用如 ...

  7. JVM内存的划分

    JVM内存的划分有五片: 1.   寄存器: 2.   本地方法区: 3.   方法区: 4.   栈内存: 5.   堆内存.

  8. c++ 获取字符串中最长的回文子串

    #include <vector> #include <iostream> #include <string> using namespace std; strin ...

  9. EasyEarth三维可视化解决方案——智慧河长

    EasyEarth—— 为河长装上“千里眼.顺风耳” 为各级河长办应急指挥.任务指派. 实绩考核提供快速直观的 高效.精准.智能化决策平台. 河长制背景 我国治水工作呈现出新老问题交织态势,河湖管理保 ...

  10. MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法

    MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法 事情的起因     今天课堂上要展示小组项目,需要用一个软件叫W ...