1

试图在 Polymer 2 元素中复制https://www.webcomponents.org/element/PolymerElements/iron-form/demo/demo/index.html中的铁形演示,但似乎无法得到它工作。

单击提交时,我得到Uncaught ReferenceError: _delayedSubmit is not defined. 有什么建议么?

JSBin : https://jsbin.com/pinasum/edit?html,console,output

代码

<dom-module id="spp-login">
    <template>
        <iron-form id="loginForm">
            <form action="/login" method="post">
                <paper-input name="username" label="Username" required auto-validate></paper-input>
                <paper-input name="password" label="Password" required auto-validate></paper-input>

                <paper-button raised onclick="_delayedSubmit(event);" disabled id="loginFormSubmit">
                    <paper-spinner id="spinner" hidden></paper-spinner>
                    Submit
                </paper-button>
                <paper-button raised onclick="loginForm.reset();">Reset</paper-button>
            </form>
            <div class="output"></div>
        </iron-form>
    </template>

    <script>
      class SppLogin extends Polymer.Element {

        static get is() {
          return 'spp-login';
        }

        static get properties() {
          return {
            username: String,
            password: String,
          };
        }

        connectedCallback() {
          super.connectedCallback();
          const loginForm = this.$.loginForm;
          const spinner = this.$.spinner;
          const loginFormSubmit = this.$.loginFormSubmit;

          loginForm.addEventListener('iron-form-submit', (event) => {
            this.querySelector('.output').innerHTML = JSON.stringify(event.detail);
            spinner.active = false;
            spinner.hidden = true;
            loginFormSubmit.disabled = false;
          });
          loginForm.addEventListener('change', (event) => {
            loginFormSubmit.disabled = !loginForm.validate();
          });

          loginForm.addEventListener('iron-form-presubmit', (event) => {
            event.preventDefault();
            console.log('here');
          });
        }

        _delayedSubmit(event) {
          const loginForm = this.$.loginForm;
          const spinner = this.$.spinner;
          spinner.active = true;
          spinner.hidden = false;
          loginForm.disabled = true;
          // Simulate a slow server response.
          setTimeout(function() {
            loginForm.submit();
          }, 1000);
        }
      }

      window.customElements.define(SppLogin.is, SppLogin);
    </script>
</dom-module>
4

2 回答 2

2

要将事件侦听器添加到 DOM 元素,您必须在模板中使用事件注释event此外,无论您是否为对象提供名称,该event对象都已被传递给您的回调。 Polymer不支持在事件属性中传递参数。

iron-form演示页面中显示的示例使用demo-snippet,它既适用于原生元素,也适用于聚合物元素。

因此,您需要将代码从:更改 onclick="_delayedSubmit(event);" 为:on-click="_delayedSubmit"

于 2017-08-14T23:38:34.793 回答
1

使用纸按钮触发事件时,您必须使用on-click并且不能指定参数。

所以正确的语法是on-click="_delayedSubmit"

于 2017-08-14T21:48:54.030 回答