2017-09-29 34 views
0
<link rel="import" href="../../bower_components/polymer/polymer-element.html"> 
    <dom-module id="my-data"> 
     <script> 
     (function() { 
     let dataValue = [ 
      {num: 'T001', status: '4', bground: 'num-sub'}, 
      {num: 'T002', status: '4', bground: 'num-sub'}, 
      {num: 'T003', status: '4', bground: 'num-sub'}, 
     ]; 
    class MyData extends Polymer.Element { 
     static get is() { return 'my-data'; } 
     static get properties() { return { 
     data: { 
      type: Array, 
      value: dataValue, 
      readOnly: true, 
      notify: true 
     } 
     }} 
    } 
    window.customElements.define(MyData.is, MyData); 
     })(); 
     </script> 
    </dom-module> 

我创建了一个像上面那样的自定义元素my-data.html。 然后下面是my-app.html中的用法。 可能会呈现我的数据,但my-app.html似乎无法通过data: Array属性获取我的数据信息。为什么Polymer自定义数据组件无法发送数据

<my-data data="{{data}}"></my-data> 
    <dom-repeat items="{{data}}" as="entry"> 
     <template> 
     <my-num entry="[[entry]]" ></my-num> 
     </template> 
    </dom-repeat> 
</template> 
    <script> 
    class MyApp extends Polymer.Element { 
     static get is() { return 'my-app'; } 
     static get properties() { 
     return { 
      data: { 
      type: Array, 
      } 
     } 
     } 
    } 
    window.customElements.define(MyApp.is, MyApp); 
    </script> 
+0

是这个吗?请参阅https://www.polymer-project.org/2.0/docs/devguide/templates#dom-repeat关于如何使用dom-repeat聚合物管理的模板。例如,请参阅https://codepen.io/johnthad/pen/zEzVLm。 – Thad

+0

如果你要改变'my-data'中数组或对象的数据,你必须通知父元素。这可以使用Polymer的'this.set(...)'以'this.set('data',dataValue)'的方式处理。请参阅https://www.polymer-project.org/2.0/docs/devguide/model-data#set-path –

回答

0

您需要导入my-data.htmlmy-num.htmlmy-app.html第一。

实施例:

<link rel="import" href="my-data.html"> 
<link rel="import" href="my-num.html"> 

此外,id名称中的my-num.htmldom-modulemy-namekvs-num

导入dom-repeat以及在您的my-app.html

<link rel="import" href="../../bower_components/polymer/lib/elements/dom-repeat.html"> 

Demo here

+0

谢谢,可以呈现my-data元素,但其他元素仍然无法从my-数据的数据属性。 – Jayly

+0

这取决于你如何在'my-app.html'中做什么?这将是不同的问题或编辑你的问题,让我知道。 – Ofisora

+0

对不起,这个问题已经更新。 – Jayly

相关问题