简体   繁体   English

如何 dom-repeat 插槽内容

[英]How to dom-repeat slot content

I'm trying create a reusable list that gets it's row template passed in as a slot but the slot content only get repeated once.我正在尝试创建一个可重用列表,将其行模板作为插槽传入,但插槽内容仅重复一次。 Example here: https://codepen.io/chris-gunawardena/project/editor/XkPYQw这里的例子: https : //codepen.io/chris-gunawardena/project/editor/XkPYQw

<script src="https://polygit.org/components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="https://polygit.org/components/paper-button/paper-button.html">
<dom-module id="list-repeater">
  <template>
      <template is="dom-repeat" items="{{items}}" as="item">
        <li>
          # [[index]]:  [[item]] <slot name="row-template"></slot>
        </li>
    </template>
  </template>
  <script>
    class ListRepeater extends Polymer.Element {
      static get is() {
        return 'list-repeater';
      }
      static get properties() {
        return {
          items: Array,
        };
      }
    }
    customElements.define(ListRepeater.is, ListRepeater);
  </script>
</dom-module>

This gets used like this:这会像这样使用:

  <list-repeater items='["a","b"]'>
    <div slot="row-template">--slot content--</div>
  </list-repeater>

The functionality is similar to iron-list which takes in a template but having a hard time following what iron-list is doing.该功能类似于 Iron-list,它接受一个模板,但很难遵循 Iron-list 正在做的事情。

Using slots will not work in your case, iron-list is using Templatizer Behavior to achieve this在您的情况下使用插槽不起作用,铁列表正在使用Templatizer Behavior来实现此目的

The Polymer.Templatizer behavior adds methods to generate instances of templates that are each managed by an anonymous Polymer.PropertyEffects instance where data-bindings in the stamped template content are bound to accessors on itself. Polymer.Templatizer行为添加了生成模板实例的方法,每个模板实例都由匿名Polymer.PropertyEffects实例管理,其中标记模板内容中的数据绑定绑定到自身的访问器。

I was also stuck with the same issue and i got one solution for this, you may try this and lemme know if this works for you or not.我也遇到了同样的问题,我得到了一个解决方案,你可以试试这个,让我知道这是否适合你。 This approach used a templatizer.这种方法使用了模板器。

HTML: HTML:

<div id="you-component-list"><your-component></you-component><div>

JS: JS:

In attached method add this line:在附加的方法中添加这一行:

attached() {
this.templatize(/** @type {!HTMLTemplateElement} **/ (
    this.$.'your-component-list'.querySelector('template')));},

const /** !Array */ flattenedNodes = Polymer.FlattenedNodesObserver.getFlattenedNodes(this); const distributedNodeToAppend = /** !Node */ flattenedNodes.find( (/** !Node */ node) => (node.nodeType === Node.ELEMENT_NODE && node.hasAttribute('slot'))); const clone = this.stamp({item: item}); if (distributedNodeToAppend) { const childNode = distributedNodeToAppend.cloneNode(true); childNode.removeAttribute('slot'); clone.root.querySelector('Your-component').appendChild(childNode); } Polymer.dom(this.$.'you-component-list').appendChild(clone.root);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM