(1)创建test.js文件 const state = { name: '测试姓名' } const mutations = { SET_NAME: (state, name) => { state.name = name } } export default { namespaced: true, state, mutations } (2)在index.js文件中引入刚才创建文件 import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' import test from './modules/test'//引入test.js文件 Vue.use(Vuex) const store = new Vuex.Store({ modules: { test }, getters }) export default store (3)getters.js文件中定义全局变量名称patient_name const getters = { patient_name: state => state.patient.name } export default getters (4)设置全局变量 this.$store.commit("patient/SET_NAME", "王小二") (5)获取全局变量 <div> 女方姓名: <label>{{this.patient_name}}</label> </div> import { mapGetters } from "vuex"; export default { data() { return { }; }, computed: { ...mapGetters(['patient_name']) }, |
|