vue----vuex如何实现在层层都可以使用$store

177 阅读1分钟

将初始化Vue根组件时传入的store设置到this对象的store属性上,子组件从其父组件引用store属性上,子组件从其父组件引用store属性,层层嵌套进行设置。在任意组件中执行 this.$store 都能找到装载的那个store对象,vuexInit方法实现如下:

将初始化vue根组件时传入的store设置到this对象的$store属性上

Vuex init hook,injected into each instances init hooks list.

function vuexInit(){ var options = this.options; //store injection if(options.store){ this.store = typeof options.store === 'function' ?options.store() :options.store; } else if (options.parent && options.parent.store){ this.store = options.parent.$store; } }

root(this.store=options.store)app(this.store=options.store) app(this.store=parent.store)productlist(this.store) productlist(this.store=parent.store)cart(this.store) cart(this.store=parent.$store)