Skip to content

Commit e71a2d3

Browse files
liusanshiyyx990803
authored andcommitted
vuex 支持延迟生成store (#798)
* vuex 支持延迟生成store 场景: 组件树:(R为根组件;C1为R的子组件;C11、C12为C1的子组件, C11,C12下面还有很多下层组件) R |___C1   |____C11   |____C12 |___C1   |____C11   |____C12 |___C1   |____C11   |____C12 C1的每个实例的数据显示是不一样的, 如果使用props传递数据,将会导致所有组件的实例要知道自己的index,不然不知道去修改哪个state。如果vuex支持延时创建store,那么在C1 上面延时生成store,下层组件使用$store的时候,不需要关心当前是哪个store的。 * Update mixin.js * 优化写法 优化写法
1 parent 1a37783 commit e71a2d3

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/mixin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export default function (Vue) {
2424
const options = this.$options
2525
// store injection
2626
if (options.store) {
27-
this.$store = options.store
27+
this.$store = typeof options.store === 'function'
28+
? options.store()
29+
: options.store
2830
} else if (options.parent && options.parent.$store) {
2931
this.$store = options.parent.$store
3032
}

0 commit comments

Comments
 (0)