【问题标题】:Vuejs nested dynamic componentVuejs 嵌套动态组件
【发布时间】:2018-11-08 15:36:51
【问题描述】:

知道我可能会有更多的孩子,有没有另一种方法可以在 VueJS 中做嵌套组件?

我事先不知道代码中会包含哪个组件,所以我使用动态组件,但它们中总会有多个子组件,而且数量并不总是相同

这是我找到的唯一解决方案 还有其他方法吗?

来自我的html:

<component :is="blocksElements[0].componentId">
    <component :is="blocksElements[0].child.componentId" v-if="blocksElements[0].hasChild">
        <component :is="blocksElements[0].child.child.componentId" v-if="blocksElements[0].child.hasChild" v-bind="blocksElements[0].child.child.props">
            <component :is="blocksElements[0].child.child.child.componentId" v-if="blocksElements[0].child.child.hasChild" v-bind="blocksElements[0].child.child.child.props"></component>
        </component>
    </component>
</component>

来自我的 js:

blocksElements: [
    {
        componentId: 'row',
        hasChild: true,
        child: {
            componentId: 'column',
            hasChild: true,
            child: {
                componentId: 'block-image',
                hasChild: true,
                props: {
                    logo: true,
                    width: '120'
                },
                child: {
                    componentId: 'block-image',
                    hasChild: false,
                    props: {
                        logo: true,
                        width: '120'
                    }
                }
            }
        }
    }
]

【问题讨论】:

    标签: javascript vue.js vuejs2 components nested-loops


    【解决方案1】:

    您可以使用渲染函数来实现:https://vuejs.org/v2/guide/render-function.html

    这是一个例子(我写得很快,所以你可以进一步改进它):

    <script>
    import Row from './Row.vue'
    import Column from './Column.vue'
    import BlockImage from './BlockImage.vue'
    
    export default {
      components: {
        Row,
        Column,
        BlockImage
      },
      data () {
        return {
          blocksElements: [
          {
            componentId: 'row',
            hasChild: true,
            child: {
              componentId: 'column',
              hasChild: true,
              child: {
                componentId: 'block-image',
                hasChild: true,
                props: {
                  logo: true,
                  width: '120'
                },
                child: {
                  componentId: 'block-image',
                  hasChild: false,
                  props: {
                    logo: false,
                    width: '130'
                  }
                }
              }
            }
          }
          ]
        }
      },
    
      render: function (h) {
        let els = []
        let result = null
        const buildElementBlocks = function (el) {
          els.unshift(el)
          if (el.hasChild) {
            return buildElementBlocks(el.child)
          }
          result = h(el.componentId, { props: el.props })
          els
            .splice(1)
            .forEach(element => {
              result = h(element.componentId, {
                props: element.props
              }, [result])
            })
        }
        buildElementBlocks(this.$data.blocksElements[0])
        return result
      }
    }
    </script>
    

    我希望这会有所帮助!

    【讨论】:

    • 如果我有一组孩子而不是只有一个孩子怎么办,我不知道如何为此实现您的代码。你能帮忙吗?
    【解决方案2】:

    是的,存在,使用树形菜单,vue.js有一个例子:https://br.vuejs.org/v2/examples/tree-view.html,这是递归。

    【讨论】:

    • 谢谢,但如果您只有一个组件,这可行,不是吗?我想不出用动态组件做到这一点的方法
    猜你喜欢
    • 2016-08-13
    • 2017-08-10
    • 2018-11-22
    • 2023-04-04
    • 2017-12-12
    • 2021-01-07
    • 1970-01-01
    • 2019-03-06
    • 2018-07-31
    相关资源
    最近更新 更多