简体   繁体   English

聚合物-自定义元素含量

[英]Polymer - Custom Element Content

I am learning Polymer. 我正在学习Polymer。 I am in the process of developing a custom element. 我正在开发自定义元素。 I have a plunk of it here . 在这里有一小撮。 My element loads some data. 我的元素加载了一些数据。 At this time, my element looks like this: 此时,我的元素如下所示:

<dom-module id="data-library">  
  <template>
    <paper-header-panel flex>
      <div class="flex">
        <template is="dom-repeat" items="[[ items ]]" as="item">
          <div>[[ item.firstName ]]</div>
        </template>
      </div>
    </paper-header-panel>
  </template>

  <script>
    Polymer({
      is: "data-library",
      properties: {
        items: {
          type: Object,
          value: function() {
            return [];
          }
        }
      },

      ready: function() {
        this.push('items', { firstName: 'John' });
        this.push('items', { firstName: 'Bill' });
      }
    });
  </script> 
</dom-module>

I want the consumer to be able to have some control over the layout of the items. 我希望消费者能够对商品的布局有所控制。 Right now, the items are always listed vertically. 现在,项目始终垂直列出。 However, I would like to let the consumer do something like: 但是,我想让消费者做类似的事情:

<data-library>
  <item-template><b>[[ item.firstName ]]</b></item-template>
</data-library>

In this scenario, the consumer is telling the custom control the item template to use. 在这种情况下,消费者告诉自定义控件要使用的项目模板。 Is there a way to do something like this in Polymer? 有没有办法在Polymer中做类似的事情? If so, how? 如果是这样,怎么办?

That's not too easy. 那不太容易。 The iron-list element does something similar. 铁表元素的作用类似。 The source should work as inspiration. 来源应作为灵感。 With iron-list you pass a <template><item-template>...</item-template>custom-content</template> 使用Iron-list,您可以传递<template><item-template>...</item-template>custom-content</template>

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

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