maioyan icon indicating copy to clipboard operation
maioyan copied to clipboard

使用vuex进行数据管理

Open yaogengzhu opened this issue 5 years ago • 0 comments

使用vuex进行数据管理

为了方便数据管理。将vuex也进行组件化开发

const state = {
    nm: window.localStorage.getItem('city_nm') || '北京',
    id: window.localStorage.getItem('city_id') || 1
}
const actions = {

}
const mutations = {
    CITY_INFO(state, params) {
        state.nm = params.nm;
        state.id = params.id;
    }
}

//  导出
export default {
// 开启命名空间
    namespaced: true,
    state,
    actions,
    mutations
}

挂载到vuex父组件上。

import Vue from 'vue'
import Vuex from 'vuex'
import city from '@/stores/city'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {

  },
  mutations: {

  },
  actions: {

  },
  modules:{
    city
  }
})

yaogengzhu avatar May 18 '19 15:05 yaogengzhu