深入浅出vuex
来源:互联网 发布:程序员刷题app 编辑:程序博客网 时间:2024/05/29 14:06
对于vuex,就像rudux的作者所说的”您自会知道什么时候需要它”
为了理解vuex,demo中运用了vuex来对底部Icon进行变化。
Vuex
Vuex是专门为 Vue.js 设计的状态管理库
如果您不打算开发大型单页应用,使用 Vuex 可能是繁琐冗余的。确实是如此——如果您的应用够简单,您最好不要使用 Vuex。
首先是创建一个 store ,里面有:
- state(状态)
- mutations(引发状态改变的方法)
- actions(触发mutations的方法)
然后在每次对数据对象进行操作的时候,进行commit(mutations 的方法名)用来触发mutations的方法来改变state状态.. 同时我们进行dispatch(action 的方法名)用来触发mutations的方法来改变state状态..
vue的组件中都有computed,当state改变的时候会触发computed,所以我们就可以根据state的值,对页面进行修改。
- 先声明一个Store:
const vuex_store = new Vuex.Store({ state: { index:1 }, mutations: { ADD_INDEX(state) { state.index++; }, }, actions:{ addIndex({commit}) { commit('ADD_INDEX') }, }})
然后在组件中写触发mutation:
this.$store.commit('ADD_INDEX')
这样就可以从组件中修改state的值。如果想要在组件中监听state的变化,来动态改变一些值:
computed : { index(){ return this.$store.state.index },}
<h1>{{index}}</h1>
同时我们也可以通过action,来触发mutation的方法来改变状态。
this.$store.dispatch("addIndex");
0 0
- 深入浅出vuex
- vuex
- vuex
- vuex
- Vuex
- vuex
- vuex
- vuex
- vuex
- vuex
- Vuex
- Vuex
- vuex
- vuex
- vuex
- vuex
- Vuex 笔记
- Vuex文档
- 简单的block使用
- C++(笔记)子类构造器初始化&执行顺序问题
- 近几年目标跟踪算法发展综述(中)
- matlab中的三种2维卷积运算
- 在WEB应用中使用spring
- 深入浅出vuex
- fragment+viewpager+FragmentPagerAdapter使用随记
- Ubuntu error: insufficient permissions for device(解决adb shell问题)
- 自动化测试面试2
- Microsoft办公软件在Mac上打开特别慢怎么办
- 策略模式
- 杭电ACM2017:字符串统计
- spark graphx joinvertices和outerjoinvertices简介
- <OpenCV1> 简单滤波器+波纹函数