博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vuejs组件交互 - 03 - vuex状态管理实现组件交互
阅读量:7002 次
发布时间:2019-06-27

本文共 949 字,大约阅读时间需要 3 分钟。

组件交互模式的使用场景

  1. 简单应用直接使用props down,event up的模式就可以了
  2. 小型应用使用事件中心模式即可
  3. 中大型应用使用vuex的状态管理模式

vuex

包含要管理的应用数据和更新数据的methods,其他组件使用 store.commit('increment')即可完成数据操作.

// Make sure to call Vue.use(Vuex) first if using a module systemconst store = new Vuex.Store({  state: {    count: 0  },  mutations: {    increment (state) {      state.count++    }  }})

现在,你可以通过 store.state 来获取状态对象,以及通过 store.commit 方法触发状态变更:

store.commit('increment')console.log(store.state.count) // -> 1

live demo

{

{ count }}

// make sure to call Vue.use(Vuex) if using a module systemconst store = new Vuex.Store({  state: {    count: 0  },  mutations: {    increment: state => state.count++,    decrement: state => state.count--  }})new Vue({  el: '#app',  computed: {    count () {        return store.state.count    }  },  methods: {    increment () {      store.commit('increment')    },    decrement () {        store.commit('decrement')    }  }})

转载地址:http://ykytl.baihongyu.com/

你可能感兴趣的文章
微信公众平台开发(43)火车时刻查询
查看>>
NetBeans 时事通讯(刊号 # 148 - May 28, 2011)
查看>>
mediascanner流程
查看>>
Linux学习笔记--进程间通信
查看>>
为什么java web项目中要使用spring
查看>>
初赛小知识之存储器
查看>>
Chosen三级联动
查看>>
node安装和npm全局配置
查看>>
python新式类与旧式类
查看>>
Android网络
查看>>
逆向工程
查看>>
C语言-计数排序
查看>>
java.lang.reflection打印一个类的全部信息
查看>>
IT行业工作6年回顾
查看>>
从零开始--系统深入学习android(实践-让我们开始写代码-Android框架学习-7.App Widgets)...
查看>>
js 事件模型
查看>>
Ubuntu 16.04 不能用inittab 设置 运行等级 runlevel
查看>>
asp.net源码坊2015-3月第二周TOP10下载排行
查看>>
看啦这么就别人的博客 我也来写一篇! Object转换其他类型
查看>>
UICollectionView官方使用示例代码研究
查看>>