2017-04-16 88 views
0



我对自定义聚合物元素有疑问。首先,我用简单的纸张输入制作了一个元素。我的问题是,我不知道如何将此元素用作“独立”元素。我的例子在这里jsfiddle。输入第一个输入“asd”,然后按Enter,然后在第二个输入“asd”中按Enter键。可以看到,这两个元素都共享属性(控制台日志“-1”不是在阵列和第二日志中找到将是“1”)

具有独立属性的多聚合物2元素?

<!doctype html> 
<html> 
    <head> 
    <title>2.0 preview elements</title> 
    <base href="http://polygit.org/polymer+v2.0.0-rc.4/webcomponentsjs+webcomponents+v1.0.0-rc.6/shadycss+webcomponents+1.0.0-rc.2/paper*+polymerelements+:2.0-preview/iron*+polymerelements+:2.0-preview/app*+polymerelements+:2.0-preview/neon*+polymerelements+:2.0-preview/components/"> 
    <script src="webcomponentsjs/webcomponents-lite.js"></script> 
    <link rel="import" href="polymer/polymer.html"> 
    <link rel="import" href="paper-input/paper-input.html"> 
    </head> 
    <body> 

    <input-test></input-test> 
    <input-test></input-test> 

    <dom-module id="input-test"> 
     <template> 
     <paper-input value="{{aValue}}" on-keydown="_keydown"></paper-input> 
     </template> 
     <script> 
     window.addEventListener('WebComponentsReady', function() { 
      class InputTest extends Polymer.Element { 
      static get is() { 
       return 'input-test'; 
      } 

      static get properties() { 
       return { 
       aValue: { 
        type: String, 
        value: '' 
       }, 
       _inputStore: { 
        type: Array, 
        value: [] 
       } 

       }; 
      } 

      _keydown(event) { 
       const keyCode_enter = '13'; 
       if (event.keyCode == keyCode_enter) { 
        console.log(this._inputStore.indexOf(this.aValue)) 
        this.push('_inputStore', this.aValue); 
       } 
      } 
      } 
      customElements.define(InputTest.is, InputTest); 
     }) 

     </script> 
    </dom-module> 
    </body> 
</html> 



我能做些什么,有独立属性?

谢谢!

回答

1

我找到了答案。

问题是数组的默认值声明。

_inputStore: { 
    type: Array, 
    value: function() { 
     return []; 
    } 
} 

这段代码解决了这个问题。