import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
let defaultCity = '上海'
try{
if(localStorage.city){
defaultCity = localStorage.city
}
} catch(e){}
export default new Vuex.Store({
state:{
city:defaultCity
},
actions:{
changeCity(ctx,city){
ctx.commit('changeCity',city)
}
},
mutations:{
changeCity(state,city){
state.city = city
localStorage.city = city
}
}
})
//调用
{{ this.$store.state.city }}
//执行
this.$store.dispatch('changeCity',city)
vue vuex使用
最新推荐文章于 2023-05-22 14:44:20 发布