簡體   English   中英

具有獨立特性的多種聚合物2元素?

[英]Multiple polymer 2 elements with independent properties?



我對自定義聚合物元素有疑問。 首先,我用一個簡單的紙張輸入就創建了一個元素。 我的問題是,我不知道如何將該元素用作“獨立”元素。 我的示例在此jsfiddle中。 輸入第一個輸入“ asd”並按Enter,然后輸入第二個輸入“ asd”並按Enter。 您可以看到,兩個元素都共享屬性(在陣列中找不到控制台日志“ -1”,第二個日志為“ 1”)

<!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>



我要怎么做才能擁有獨立的屬性?

謝謝!

我找到了答案。

問題是數組的默認值聲明。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM