VUEX是什么?vue
用来解决组件之间共用数据问题的一个插件
Vuex 内部结构vuex
Components 与Vuex的交互学习
在该文件中引入vue和vuex,并在vue中启用vuexspa
Improve vue from 'vue' Improve vuex from 'vuex' vue.use(vuex)
在该文件中配置 state,mutations,actions插件
//这里的state是数据实际储存的地方 const state={ count:10 } const mutations={ add(state,param){ state.count += param }, reduce(state,param){ state.count -= param } } const actions={ add:({commit},param)=>{ commit('add',param) }, reduce:({commit},param)=>{ commit("reduces",param) } }
只有一个store配置的方法code
将以上配置在Vuex对象中实例化并导出对象
export default new vuex.Store({ state, mutations, actions })
在main.js中引用该vuex的store实例it
improt store from './store' new vue ({ ...... store, ...... })
在组件中使用vuex的store实例
在页面中引用该实例state的值 $store.state.count
引入该实例的actions io
import {mapActions} from ‘vuex’ export default { methods:mapActions([‘add’,’reduce’]) }
页面使用actions @click='add(param)'
@click='reduce(param)'
class
有多个store配置的方法
将以上配置在Vuex对象中实例化并导出
export default new vuex.Store({ module:{ a: { state, mutations, actions } } })
在main.js中引用该vuex的store实例
improt store from './store' new vue ({ ...... store, ...... })
在组件中使用vuex的store实例
在页面中引用该实例state的值 $store.state.a.count
调用该实例的actions
import {mapActions} from ‘vuex’ export default { methods:mapActions('a',[‘add’,’reduce’]) }
页面使用actions @click='add(param)'
@click='reduce(param)'
这篇笔记主要是本人学习Vuex时候的知识总结,若是有不对的地方还请多多斧正