简体   繁体   English

来自子递归的Vue组件父包含

[英]Vue component parent from child recursive include

So I want to have many components nested to each other and included dynamically.所以我想让许多组件相互嵌套并动态包含。

Lets assume simple case:让我们假设简单的情况:

-container -row -container -row -widget

etc.等等

So how can I include container that will load row which will load previous component container in an elegant way (recursive I guess)那么如何包含将加载行的容器,该行将以优雅的方式加载先前的组件容器(我猜是递归的)

I want this functionality for more components than just container and row我希望此功能适用于更多组件,而不仅仅是containerrow

I had the same problem myself right now: it's usually webpack which cause this problem so you have two options:我自己现在也遇到了同样的问题:通常是 webpack 导致了这个问题,所以你有两个选择:

  1. Register your component Globally在全球范围内注册您的组件

  2. On you child component, register the parent like this:在您的子组件上,像这样注册父组件:

    components: { ComponentLoader: () => import('./ComponentLoader.vue') }组件:{ ComponentLoader: () => import('./ComponentLoader.vue') }

you can read more here: https://v2.vuejs.org/v2/guide/components-edge-cases.html#Circular-References-Between-Components你可以在这里阅读更多: https ://v2.vuejs.org/v2/guide/components-edge-cases.html#Circular-References-Between-Components

So to achieve my goal I have to build ComponentLoader and child components loaded from it.因此,为了实现我的目标,我必须构建 ComponentLoader 和从中加载的子组件。

ComponentLoader.vue
<template
    v-for="(block, index) in data.children">
    <component
      v-if="component"
      :is="component"
      :key="`container-${block.id}-${index}`"
      :data="block"/>
  </template>
</template>

Which for instance will load Article component from its children:例如,它将从其子项加载 Article 组件:

ArticleComponent.vue
<template>
  <SimpleArticle
    :data="data"/>
  <ComponentLoader
    v-if="data.children"
    :data="data"/>
</template>

So ArticleComponent will call ComponentLoader again if it has more children to load.因此,如果ArticleComponent有更多要加载的子项,它将再次调用ComponentLoader This way works for me and is recursively going through the data tree.这种方式对我有用,并且递归地遍历数据树。

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

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