场景: 通过state中的一个值来切换显示 对应的 modal 一开始直接使用$store.state.globalState
无法在模块内实现以下效果
<div v-show="$store.state.globalState =='医生'">
<MedicalCompaniesList ></MedicalCompaniesList>
</div>
<div v-show="$store.state.globalState =='教师'">
111
</div>
最终想到mapGetters
const state = {
globalState:'医生'
}
const getters = {
getGlobalState(state){
return state.globalState
},
}
const mutations = {
setGlobalState(state,data){
state.globalState= data
}
}
const store = new Vuex.Store({
state,
getters,
mutations,
modules: {},
})
(.vue)组件内使用
import {mapGetters} from 'vuex'
computed:{
...mapGetters(['getGlobalField'])
},
watch: {
getGlobalField: function(newVal) {
}
},
最终实现根据globalState切换显示