vuex store 的使用

store不仅能在组件中通过this来使用,获取数据,提交修改,也可以在其他文件或模块中使用

导出store时,需要导出一个实例化的对象,这样在其他文件中引入的也就是同一个对象了,即可以同步操作数据

const store = new Vuex.Store({
  state,
  mutations,
  // getters,
  strict: true,
  plugins: [logger()],
  // actions
})
export default store

api/index.js,中引入store实例,提交修改

import store from '../store'

const add = () => store.commit('add', 2)

export {
  add,
}

在别处只需要调用这个api就行了,和在组件中提交修改是一样的

 import {add} from './api'
 add()

猜你喜欢

转载自my.oschina.net/ahaoboy/blog/1636117