From 155f56eed8e83cde1312d3af00eb3df2dc26a514 Mon Sep 17 00:00:00 2001 From: sanshi Date: Wed, 24 May 2017 10:23:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?vuex=20=E6=94=AF=E6=8C=81=E5=BB=B6=E8=BF=9F?= =?UTF-8?q?=E7=94=9F=E6=88=90store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 场景: 组件树:(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的。 --- src/mixin.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mixin.js b/src/mixin.js index 8c6c14d39..8239e2c01 100644 --- a/src/mixin.js +++ b/src/mixin.js @@ -24,6 +24,9 @@ export default function (Vue) { const options = this.$options // store injection if (options.store) { + if (typeof options.store === 'function') { + options.store = options.store(); + } this.$store = options.store } else if (options.parent && options.parent.$store) { this.$store = options.parent.$store From 29b6c77ec016ad3ffae656bcf65689c28067cb7b Mon Sep 17 00:00:00 2001 From: sanshi Date: Wed, 24 May 2017 10:29:08 +0800 Subject: [PATCH 2/3] Update mixin.js --- src/mixin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mixin.js b/src/mixin.js index 8239e2c01..0a01a1336 100644 --- a/src/mixin.js +++ b/src/mixin.js @@ -25,7 +25,7 @@ export default function (Vue) { // store injection if (options.store) { if (typeof options.store === 'function') { - options.store = options.store(); + options.store = options.store() } this.$store = options.store } else if (options.parent && options.parent.$store) { From eab70025654ac3580b10869e5c28156cdfaded1e Mon Sep 17 00:00:00 2001 From: sanshi Date: Fri, 26 May 2017 09:24:01 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化写法 --- src/mixin.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mixin.js b/src/mixin.js index 0a01a1336..3d5c5fb49 100644 --- a/src/mixin.js +++ b/src/mixin.js @@ -24,10 +24,9 @@ export default function (Vue) { const options = this.$options // store injection if (options.store) { - if (typeof options.store === 'function') { - options.store = options.store() - } - this.$store = options.store + this.$store = typeof options.store === 'function' + ? options.store() + : options.store } else if (options.parent && options.parent.$store) { this.$store = options.parent.$store }