Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vuex 支持延迟生成store #798

Merged
merged 3 commits into from
May 26, 2017
Merged

vuex 支持延迟生成store #798

merged 3 commits into from
May 26, 2017

Conversation

liusanshi
Copy link
Contributor

场景:
组件树:(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的。

场景:
组件树:(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的。
@Sobranier
Copy link

Sobranier commented May 24, 2017

"如果vuex支持延时创建store,那么在C1 上面延时生成store,下层组件使用$store的时候,不需要关心当前是哪个store的。"这句话是什么意思?

ps:I suggest u to write in Englist, cause mamy developers are foreigners

src/mixin.js Outdated
@@ -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()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just do

this.$store = typeof options.store === 'function'
  ? options.store()
  : options.store

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以

优化写法
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants