简体   繁体   English

使用参数化模板进行dom-repeat

[英]dom-repeat with parameterized template

What is the clean way , with Polymer 2.0 , to parameterize the dom-repeat item template ? 使用Polymer 2.0 ,参数化dom-repeat项目模板的干净方法是什么?

Usage: 用法:

<custom-component>
    <template id="item-template">
        [[item]]
    </template>
</custom-component>

CustomComponent: CustomComponent:

<dom-module id="custom-component">
    <template>
        <template is="dom-repeat" items="[[foo]]" id="repeater">
            <!-- Parameterized template -->
        </template>
    </template>
    <!-- scripts... -->
</dom-module>

I cannot find any clear documentation on Polymer 2.0 to achieve this. 我找不到有关Polymer 2.0的任何明确文档来实现这一目标。

I just new to learn polymer.Maybe I just your question.I guess you want to repeat an element which has the structure and data depends on want you need it to be. 我只是新学习聚合物。也许我只是你的问题。我想你想重复一个具有结构和数据的元素取决于你是否需要它。 And there are 3 points. 并且有3分。

demo-element.html 演示element.html

<dom-moudle is="demo-element">
  <template>
    <span>{{property1.name}}</span>
    <span>{{property1.age}}</span>
    <script>
      class DemoElement extends Polymer.Element {
        static get is() { return 'demo-element'; }
        static get properties() {
          return {
            property1: Object
          }
        }
      }
      window.customElements.define(DemoElement.is, DemoElement);
    </script>
  </template>
</dom-moudle>

custom-component.html 定制component.html

<dom-module id="custom-component">
  <template>
    <template is="dom-repeat" items="[[foo]]" id="repeater">
        <demo-element property1="[[item]]"></demo-element>
    </template>
  </template>
</dom-module>
  1. Dom-repeat's item property will be set on each instance's binding scope ,so you can not set dom-repeat's item property in custom-component's instance. Dom-repeat的item属性将在每个实例的绑定范围上设置 ,因此您无法在custom-component's实例中设置dom-repeat的item属性。

  2. Create a custom element with the structure and data as you need.Repeat the custom element with dom-repeat . 根据需要使用结构和数据创建自定义元素。使用dom-repeat自定义元素。

  3. slot element is always suitable for Polymer.If you need to let custom-component include some other elements you should use placeholder. slot元素总是适合Polymer.If你需要让custom-component包含一些其他元素,你应该使用占位符。

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

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