vuex 的简单使用

vuex是一个专门为vue.js设计的集中式状态管理架构。这篇文章主要介绍了vuex 的简单使用,需要的朋友可以参考下

什么是Vuex?

vuex是一个专门为vue.js设计的集中式状态管理架构。状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态。简单的说就是data中需要共用的属性。

1.在vue 组件中

执行enabledcheckbox方法 ,true 为参数,用来改变state中的值

 this.$store.dispatch("enabledcheckbox",true) 

从state获取useredit的值

 this.$store.state.useredit

2 在vuex导出的对象对添加 值到state

添加 mutations 来改变state的值

添加 actions 来 mutations

 import Vue from 'vue' import vuex from 'vuex' Vue.use(vuex) export default new vuex.Store({ state: { useredit: false, }, mutations: { ENABLEDCHECKBOX(state, value) { state.checkboxDisable = value }, }, actions: { enabledcheckbox({ commit }, value) { commit('ENABLEDCHECKBOX', value) }, } }) //console.log(vuex)

在main.js

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

总结

以上就是vuex 的简单使用的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Vue.js 教程