const state = {
count: 0,
arr: [0,1,2,3,4,5]}
export default state;
如此这般,在main.js中,我们需要编写的代码就减少了很多
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index'; Vue.config.productionTip = false
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
将vuex实例挂载到vue原型链上
这是一种非主流的方式,主要是受axios启发,这里有一篇博客讲解如何在vue组件中使用axios,将axios挂载到vue原型链上是因为不能通过vue.use来使用axios
在这种方法中,我们需要
// The Vue build version to load with the `import` command
// (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 store from './store/index'; Vue.config.productionTip = false
//在vue中使用vuex必须先调用vue.use方法
Vue.use(Vuex);
//具体挂载到vue原型的哪个属性上,可以由我们自行决定
//遇到配置繁多的情况也可以进行分割
Vue.prototype.$store = new Vuex.Store({
state: {},
getters: {},
actions: {},
mutations: {}
});
//没有了store选项
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
如此这般,还是可以通过
this.$store 来使用vuex










