首先在根目录下创建store目录在里面创建index.js
index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {},
mutations: {},
actions: {}
})
export default store
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()
页面中使用
<script>
export default {
onShow () {
console.log(this.$store.xxx)
}
}
</script>