【问题标题】:Is it possible to have nested knockout components in javascriptjavascript中是否可以嵌套淘汰组件
【发布时间】:2015-03-06 15:43:05
【问题描述】:

我想要这样的东西:

<base-component>
    <sec-component>
    </sec-component>
</base-component>

是否有可能在淘汰组件的帮助下实现这一点?

【问题讨论】:

  • 来自documentation…可以组合在一起(嵌套)或从其他组件继承
  • 通过投票/接受表示感谢。公告不属于问题(没有分心,没有闲聊(阅读帮助→tour

标签: knockout.js knockout-components


【解决方案1】:

是的。在文档中,“将标记传递给组件”部分:http://knockoutjs.com/documentation/component-custom-elements.html#passing-markup-into-components

<!-- This could be in a separate file -->
<template id="my-special-list-template">
    <h3>Here is a special list</h3>

    <ul data-bind="foreach: { data: myItems, as: 'myItem' }">
        <li>
            <h4>Here is another one of my special items</h4>
            <!-- ko template: { nodes: $componentTemplateNodes, data: myItem } --><!-- /ko -->
        </li>
    </ul>
</template>

<my-special-list params="items: someArrayOfPeople">
    <!-- Look, I'm putting markup inside a custom element -->
    The person <em data-bind="text: name"></em>
    is <em data-bind="text: age"></em> years old.
</my-special-list>

li 元素内的ko template 中,将添加节点。

因此,您还可以在内部标记中插入不同的组件。 例如:

<!-- This could be in a separate file -->
<template id="my-special-list-template">
    <h3>Here is a special list</h3>

    <ul data-bind="foreach: { data: myItems, as: 'myItem' }">
        <li>
            <h4>Here is another one of my special items</h4>
            <!-- ko template: { nodes: $componentTemplateNodes, data: myItem } --><!-- /ko -->
        </li>
    </ul>
</template>

<template id="my-person-template">
    The person <em data-bind="text: name"></em>
    is <em data-bind="text: age"></em> years old.
</template>

<my-special-list params="items: someArrayOfPeople">
    <my-person></my-person>
</my-special-list>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 2012-10-14
    • 1970-01-01
    • 2015-12-21
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    相关资源
    最近更新 更多