1,安装
npm install vuex --save
2,在src下新建store文件夹,且新建index.j
3,store/index.js里代码如下
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); export default new Vuex.Store({ modules: { }, getters })
4,在main.js中引入全局stroe;
// (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css'; import store from './store/'; //引入store Vue.config.productionTip = false; Vue.use(ElementUI); /* eslint-disable no-new */ new Vue({ el: '#app', router, store, // 全局使用 components: { App }, template: '<App/>' })
5,在需要用的地方通过 this.$store.state.xxx;获取需要的缓存值。打印this.$store得到如下;
代表引入成功!