【问题标题】:VueJS - Load child components dynamicallyVueJS - 动态加载子组件
【发布时间】:2017-08-10 12:36:07
【问题描述】:

我试图弄清楚如何从 Laravel 的刀片模板有条件地加载子/子组件

  1. 我有一个从刀片调用的主容器:

    <structure-container view='menu-index'></structure-container>

  2. 这里是 StructureContainer.vue

    <template>
    <div>
        //I can have if-else statement here by "view", but looking for a better solution
        <menu-index></menu-index>
    </div>
    

    export default{
        components: {
            'menu-index': require('./menu/IndexComponent.vue'),
            'test': require('./menu/TestComponent.vue'),
        },
        props: ['view']
    }
    

如您所见,我有两个子组件:菜单索引和测试。我想从刀片传递参数并显示相应的组件。我可以避免 if-else 语句吗?应该有更好的方法来做到这一点

【问题讨论】:

    标签: laravel vuejs2 laravel-blade


    【解决方案1】:

    您可以简单地将组件名称动态绑定到is 属性(使用v-bind:is 或:is),即:

    <div>
        <component :is="view"></component >
    </div>
    

    真正令人敬畏的是&lt;component&gt; 容器是反应式的,您可以动态加载/卸载/更改组件。这有多棒? :)

    如需更多信息,我鼓励您阅读documentation on dynamic components on the official docs。

    【讨论】:

    • 谢谢特里,有道理。如果直接绑定到道具“视图”&lt;component :is="view"&gt;&lt;/component &gt;,它也可以工作
    • @GeorgeSharvadze 很高兴它成功了。是的,如果你直接将它绑定到道具上,这也将起作用。我会更新我的答案以反映这一点;)
    猜你喜欢
    • 2018-09-13
    • 2017-07-30
    • 1970-01-01
    • 2014-11-23
    • 2017-09-21
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2019-09-20
    相关资源
    最近更新 更多