1.store.js结构

import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({
state: {
  city: '汉中'
},
mutations: { },
actions: { }
})

2.main.js引入

import store from './store'
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')

3.组件使用State的数据

{{ this.$store.state.city }}

4.用dispatch调用Actions,Actions用commit调用Mutations修改数据

具体组件.vue
methods: {
handleClick (city) {
this.$store.dispatch('changeCity', city)
}
}
store.js
import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({
state: {
city: '汉中'
},
mutations: {
changeCity (state, city) {
state.city = city
}
},
actions: {
changeCity (ctx, city) {
// ctx为上下文,city是传来的参数
ctx.commit('changeCity', city)
}
}
})

5.数据简单,跳过Actions,直接到Mutations

具体组件.vue
methods: {
handleClick (city) {
this.$store.commit('changeCity', city)
}
}
store.js
import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({
state: {
city: '汉中'
},
mutations: {
changeCity (state, city) {
state.city = city
}
}
})

最新文章

  1. MFC 关于MFC中CImage的简单使用
  2. angular 自定义指令 link
  3. MVC缓存技术
  4. TF-IDF算法扫盲2
  5. bzoj4046
  6. uva 10651 - Pebble Solitaire(记忆化搜索)
  7. 【Python网络爬虫四】通过关键字爬取多张百度图片的图片
  8. 安装ARM交叉编译器
  9. JBoss7 如何用脚本 启动 和 停止
  10. Linux学习历程——Centos 7 chmod命令
  11. Python基础之函数参数
  12. ComboBox下拉列表框
  13. mycat 测试主从读写分离
  14. XML异常
  15. centos7如何查看网络状态?
  16. hdu 2844 coins(多重背包 二进制拆分法)
  17. How to use Jackson to deserialise an array of objects
  18. basename 命令
  19. Azure SQL 数据库仓库Data Warehouse (3) DWU
  20. vc 读xml文件 宏

热门文章

  1. tensorflow的变量作用域
  2. 【CF1250G】Discarding Game(DP)
  3. 第五周课程总结&试验报告
  4. Linux下服务器开发的必要准备
  5. listen( ) accept( )
  6. 后盾网lavarel视频项目---laravel 使用laracasts/flash插件提示信息
  7. 字符串在PHP比较运算中的变化
  8. 对vi/vim的一些看法
  9. ifram刷新父窗口中内嵌页面
  10. linux查看硬盘信息