由於uni-app已經內置了vuex,所以只要正確引入就好了。
1、在項目的根目錄下,創建一個名為store的文件夾然后在該文件夾下創建一個index.js的js文件
2、在該js文件下定義公共的數據以及方法函數,並且把它導出
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: {}, mutations: {}, actions: {} }) export default store
3、在入口文件即:main.js掛載vuex
import Vue from 'vue' import App from './App' //引入vuex import store from './store' //把vuex定義成全局組件 Vue.prototype.$store = store Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ ...App, //掛載 store }) app.$mount()
4、在單頁面里使用vuex
<script> export default { created () { console.log(this.$store) } } </script>
作者:Rogi
鏈接:https://www.jianshu.com/p/c288b8e6067c
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。