【问题标题】:Polymer 2.0 using Shady DOM document.getElementByID()Polymer 2.0 使用 Shady DOM document.getElementByID()
【发布时间】:2017-12-12 09:49:39
【问题描述】:

我通过以下脚本在 Polymer 2.0 中使用 Shady DOM

<script>window.ShadyDOM = {force:true};</script>

我创建了三个自定义元素登录电子邮件、登录密码和登录按钮。单击纸张按钮时,我想获取登录电子邮件和登录密码的纸张输入值。使用 Polymer 1.0,我使用 document.getElementById('#emailLogon').value 从另一个自定义元素获取值,但这在 Polymer 2.0 中使用 Shady DOM 返回 null。

如果我现在无法从另一个自定义元素中检索来自外部自定义元素的值,还有什么替代方法。

<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="shared-styles.html">

    <link rel="import" href="../bower_components/paper-input/paper-input.html">

    <dom-module id="logon-email">
      <template> 
        <style include="shared-styles">
        :host {
            display: block;

            padding: 10px;

          }

          paper-input {
            --paper-input-container-input-color: white; 
            --paper-input-container-label: { color: white; font-size: 12px};
          }

          .email_label {
            font-family: 'Roboto', 'Noto', sans-serif;
            font-size:      12px;  
            color: white;           
          }

        </style>
        <div class="email_label">Email</div>
        <paper-input label="Please enter your email address" no-label-float></paper-input>

      </template> 

      <script>
        class LogonEmail extends Polymer.Element {
          static get is() { return 'logon-email'; }
        }
        window.customElements.define(LogonEmail.is, LogonEmail);
      </script>
    </dom-module>

<dom-module id="logon-password">
  <template>
    <style include="shared-styles">
      :host {
        display: block;

        padding: 10px;

      }

      paper-input {
        --paper-input-container-input-color: white;
        --paper-input-container-label: { color: white; font-size: 12px; };    
      }    

      .password_label {
        font-family: 'Roboto', 'Noto', sans-serif;
        font-size:      12px;  
        color: white;           
      }   

    </style>

    <div class="password_label">Password</div>
    <paper-input id="logonPassword" label="Please enter your password" type="password" no-label-float></paper-input>

  </template>

  <script>
    class LogonPassword extends Polymer.Element {
      static get is() { return 'logon-password'; }
    }

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

<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="shared-styles.html">

<link rel="import" href="../bower_components/paper-button/paper-button.html">
<link rel="import" href="../bower_components/paper-styles/color.html">

<dom-module id="logon-button">
  <template>
    <style include="shared-styles">
      :host {
        display: block;

        padding: 10px;
      }


      paper-button {
        font-family: 'Roboto', 'Noto', sans-serif;
        font-weight: normal;
        font-size: 14px;
        -webkit-font-smoothing: antialiased;
      }  

      paper-button.green {
        background-color: var(--paper-green-500);
        color: white;
        margin: auto;
        width: 100%;
      }

    </style>

    <paper-button on-click="handleLoginClick" raised class="green">Login</paper-button    

  </template>

  <script>
    class LogonButton extends Polymer.Element {
      static get is() { return 'logon-button'; }
            connectedCallback() {
              super.connectedCallback();
            }

        handleLoginClick(){
          console.log('Login button clicked');
          var loginEmail = document.getElementById('#logonEmail');

          console.log('logonEmail ' + loginEmail);
        }
    }

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

【问题讨论】:

    标签: polymer polymer-2.x shady-dom


    【解决方案1】:

    这是聚合物在元素之间传递值的最简单方法。所以只需定义属性并将其设置为 notify:true 以反映在其他元素上作为休闲:

    <paper-input label="Please enter your email address" no-label-float value="{{emailVal}}"></paper-input>
    

    在主文档中将 emailVal 属性传递给您的自定义元素,因此您在所有 3 个元素中都有属性,例如 this.emailVal

     <logon-email email-val="{{emailVal}}"></logon-email>
     <logon-password email-val="{{emailVal}}"></logon-password>
     <logon-button email-val="{{emailVal}}"></logon-button>
    

    并在 logon-email 元素中定义此属性并将其设置为 notify : true 以反映任何更改中的所有属性。 在logon-email

      static get properties() { return { 
         emailVal:{
            type:String,
            notify:true
       },...
    

    logon-button 元素处:

     handleLoginClick(){
          console.log('Login button clicked');
          console.log('logonEmail ', this.emailVal);
        }
    

    希望清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多