Vue状态管理
2017-12-05 16:08
537 查看
1、导出Vuex
2、定义store
3、将store注入
4、store状态更改
5、子组件中获取状态 使用mapState
import Vuex from 'vuex'
2、定义store
/*状态管理*/ const store = new Vuex.Store({ state: { headerShow: true//是否显示头部 }, mutations: { //不应直接更改state,应通过mutations下的方法来更改状态 setHeaderShow(state, newValue) { this.state.headerShow = newValue; } } });
3、将store注入
new Vue({ store,//把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件 render: h => h(App) }).$mount('#app-box')
4、store状态更改
this.$store.commit('setHeaderShow', true);
5、子组件中获取状态 使用mapState
import { mapState } from 'vuex' export default { name: 'app', components: { }, computed: { ...mapState({ headerShow: state => state.headerShow }) }, }
相关文章推荐
- 前端框架Vue(10)——vuex 状态管理
- vue 状态管理vuex(九)
- [Vue.js启航]——使用Vuex进行状态管理
- 理解vuex的状态管理模式架构
- Vuex 模块化实现待办事项的状态管理
- 一篇看懂vuejs的状态管理神器 vuex状态管理模式
- Vuex 模块化实现待办事项的状态管理
- Vue 2.X的状态管理vuex记录详解
- 在Vue中使用Vuex进行状态管理指南
- Vuex 模块化实现待办事项的状态管理
- 理解Vue的状态管理模式Vuex
- vue项目用户登录状态管理,vuex+localStorage实现
- Vuex内容解析和vue cli项目中使用状态管理模式Vuex
- 理解vuex -- vue的状态管理模式
- vue的状态管理模式vuex
- Vuex 模块化实现待办事项的状态管理
- 简单实用vue状态管理vuex
- vuejs组件交互 - 03 - vuex状态管理实现组件交互
- Vue 状态管理
- vue2.0 仿手机新闻站(三)通过 vuex 进行状态管理