2017-04-16 151 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 []; 
    } 
} 

這段代碼解決了這個問題。