在vuex组件中的mutations属性中的定义的函数,有时会要用到vue这个对象。正常在其余的地方使用是经过this这个变量来获取,可是在mutations定义的函数中this指定的是Vuex这个当前对象。vue
在main.js中经过一个变量接收实例化的Vue对象,然以导出这个变量,这样在其余地方能够经过导入这个变量来使用Vue对象。vuex
// main.js const vue = new Vue({ el: '#app', router, store, components: {App}, template: '<App/>' }); export default vue;
在Vuex中使用app
import vue from '../main' export default new Vuex.Store({ mutations: { Test(state) { console.log(vue); } } })