vuejs中使用vuex的两种方案之一

vue.js使用vuex之一:
文件目录如下:
vuejs中使用vuex的两种方案之一_第1张图片
index.js文件demo

import Vue from 'vue'
import Vuex from 'vuex'

import name from './modules/name'
Vue.use(Vuex)

const crea = () => {
  return new Vuex.Store({
    modules: {
      name
    }
  })
}
export default crea

name.js文件demo如下

const state = () => {
  id: ''
  name: ''
}

const getters = {
  getId(state) {
    return state.id
  },
  getName(state) {
    return state.name
  }
}
const actions = {}
const mutations = {
  saveId(state, param) {
    state.id = param.id
    state.name = param.name
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}

存:

this.$store.commit('saveId', {id: '1000', name:'desting0725'})

取:

this.$store.getters.getId//1000
this.$store.getters.getName//desting0725

你可能感兴趣的:(技术方案)