【问题标题】:vue.js 2 how to properly nest componentsvue.js 2 如何正确嵌套组件
【发布时间】:2017-09-10 19:58:44
【问题描述】:

您能告诉我如何正确地将组件添加到其他组件吗? 下面的示例不起作用。子组件不显示在父组件中。

<div id="app">
    <parent>
        <child></child>
    </parent>
</div>

<template id='child'>
    <div>child component</div>
</template>

<template id='parent'>
    <div>parent component</div>
</template>

<script>

    var child = {
        template: '#child',
        data: function () {
            return {}
        }
    };

    var parent = {
        template: '#parent',
        data: function () {
            return {}
        }
    };

    new Vue({
        el: '#app',
        components: {
            'parent': parent,
            'child': child
        }
    })
</script>

示例:https://jsfiddle.net/05gc05sk/1/

如何正确嵌套组件?

【问题讨论】:

标签: vue.js vuejs2


【解决方案1】:

您的代码确实有效。

仅将&lt;slot&gt; (Content Distribution with Slots) 添加到您的父组件。

<template id='parent'>
  <div>
    parent component
    <slot></slot>
  </div>
</template>

【讨论】:

    猜你喜欢
    • 2018-03-22
    • 2016-07-21
    • 2020-09-09
    • 2015-11-22
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2016-08-04
    • 2018-01-26
    相关资源
    最近更新 更多