uni-app官方一開始就提供了vue的vuex不需要我們手動下載安裝
首先創(chuàng)建store文件在store文件下創(chuàng)建index.js文件
寫如下代碼
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
mast: '你好'
},
mutations: {},
getters: {},
actions: {},
})
export default store
然后再main.js配置如下代碼
import App from './App'
import Vue from 'vue'
import store from "@/store/index.js"
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
store,
...App
})
把store進行注冊
頁面進行調用通過
this.$store.state.mast
如果開發(fā)的是小程序端需要computed返回
computed: {
app() {
return this.$store.state.mast
}
},