vue.js 2如何正确嵌套组件

时间:2017-09-10 19:58:45

标签: vue.js vuejs2

您能否告诉我如何正确地将组件添加到其他组件? 以下示例不起作用。子组件不会显示在父组件内。

<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/

如何正确嵌套组件?

1 个答案:

答案 0 :(得分:1)

您的代码确实有效。

仅将<slot>Content Distribution with Slots)添加到您的父组件。

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