简体   繁体   English

Vue / Vuex-如何从子孙中检索数据

[英]Vue/Vuex - How to retrieve data from grandchildren

started with VueJs for the first time yesterday and now I'm stuck.. 昨天第一次是从VueJs开始的,现在我被困住了。

I have a parent component who has child items that also has a child inside them (I call them grandchildren). 我有一个父项,该子项中有一些子项,其中也有一个子项(我称它们为孙子项)。 I want to fetch data from all the grandchildren when i click a button in the parent but i can't figure out how. 当我单击父级中的按钮时,我想从所有孙子级中获取数据,但我不知道如何操作。

In my mind a want to call an event from parent to to all the grandchildrens that they should store their data to vuex store. 在我看来,希望从父级向所有孙级调用一个事件,即他们应该将其数据存储到vuex存储。 Is this possible somehow or is there another way to do this? 这可能以某种方式出现还是有另一种方式来做到这一点?

// Data
blocks = [
    {
        id: 1,
        type: 'HeadingBlock',
        title: 'Hello',
        color: 'blue'
    },
    {
        id: 2,
        type: 'ImageBlock',
        image_id: 2
    }
];


// App.js

<ContentBlocks :blocks="blocks" / >

// ContentBlock.vue

<ContentBlockItem v-for="(block, index) in blocks" :component="block.type" ... />

// ContentBlockItem.vue

<component :is="component" :block="block" /> // Grandchild

// component aka the grandchild (eg. HeadingBlock.vue)

data() {
  return {
    title: 'Hello - I want save the changed data for this heading',
    color: 'blue'
  }
}

So, the only call to action happens in the parent by a "save"-button. 因此,唯一的号召性用语是通过“保存”按钮在父级中发生的。 And i want as little logic in grandchildren as possible (to make it easy to create new ones, like a "ParagraphBlock"). 而且我希望子孙中的逻辑尽可能少(以使其易于创建新的逻辑,例如“ ParagraphBlock”)。

Thx in advance 提前Thx

It is possible to emit() a global Event that all your components subscribe to - however it seems rather impractical. 可能会emit()一个您所有组件都订阅的全局Event -但是这似乎是不切实际的。 (eg this.$root.emit('saveData') in parent; in all children to listen to it: this.$root.on('saveData') ) (例如,父母中的this.$root.emit('saveData') ;在所有要听的孩子中: this.$root.on('saveData')

A more practical approach is to store all your component data in the store in the first place. 一种更实用的方法是首先将所有组件数据storestore中。 And have each component retrieve its state from the store. 并让每个组件从存储中检索其状态。 (eg in a computed property: title() { return this.$store.myComponent.title } . (例如,在计算属性中: title() { return this.$store.myComponent.title }

The trick here is obviously to set all your store-data correctly (eg with componentIDs to match it correctly). 显然,这里的窍门是正确设置所有商店数据(例如,使用componentID正确匹配)。 To do this you need to be aware, that vuex does not support maps or sets . 为此,您需要知道,vuex不支持mapssets Also, you have to set each property/element individually - you cannot set nested structures in one go bu thave to do it recursively. 另外,您必须分别设置每个属性/元素-您不能一次设置嵌套结构来递归地进行设置。 Hereby Arrays have to be filled with native array methods ( push(), splice() ) and Object properties have to be set with Vue.set(object, key, value) . 因此, Arrays必须使用本机数组方法( push(), splice() )填充,并且必须使用Vue.set(object, key, value)设置Object属性。

For accessing data between parent and child component you can use one of the best features of vue is vuex store. 为了在父组件和子组件之间访问数据,您可以使用vue的最佳功能之一就是vuex store。 It's really helpful when you want to pass data to child component and update that data in child and again pass back to parent without the use of props and event emit. 当您想要将数据传递给子组件并在子组件中更新该数据并再次传递回父组件而不使用道具和事件发射时,这确实很有用。

Here is a link you can follow for your web application 这是您可以为您的Web应用程序链接的链接

https://medium.com/dailyjs/mastering-vuex-zero-to-hero-e0ca1f421d45 https://medium.com/dailyjs/mastering-vuex-zero-to-hero-e0ca1f421d45

https://medium.com/vue-mastery/vuex-explained-visually-f17c8c76d6c4 https://medium.com/vue-mastery/vuex-explained-visually-f17c8c76d6c4

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM