uni-app已经内置了vuex,所以只要正确引入就好了。
1、根目录创建store文件存放vuex
2、创建index.js
导入vue和vuex
import Vue from 'vue' import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
city:'城市名',
login:false
},
mutations: {
ShowGood(){
console.log('that is good!!!')
}
},
actions: {}
})
export default store
3、main.js入口文件
// 引入store import store from './store' // 把vuex定义成全局组件 Vue.prototype.$store = store
导入辅助函数
import { mapState, mapMutations } from 'vuex'
使用
computed: { ...mapState(['login', 'city']) },
methods: { change(e) { this.current = e.detail.current; }, ...mapMutations(['ShowGood']) }
人生很漫长,或许我只是你人生中微不足道的一小段,只是你人生中的惊鸿一瞥。
浙公网安备 33010602011771号