Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在使用来自同级聚合元素的dispatchEvent时元素不捕获事件?_Javascript_Polymer_Polymer 2.x - Fatal编程技术网

Javascript 为什么在使用来自同级聚合元素的dispatchEvent时元素不捕获事件?

Javascript 为什么在使用来自同级聚合元素的dispatchEvent时元素不捕获事件?,javascript,polymer,polymer-2.x,Javascript,Polymer,Polymer 2.x,我的UI中有以下聚合元素,它有两个聚合元素:基线策略创建和基线策略域ajax <dom-module id="baseline-policies-tab"> <template> <style include="dfw-styles"> :host { display: block; } </style> <baseline-policy-create></bas

我的UI中有以下聚合元素,它有两个聚合元素:基线策略创建和基线策略域ajax

<dom-module id="baseline-policies-tab">
  <template>
    <style include="dfw-styles">
      :host {
        display: block;
      }
    </style>
    <baseline-policy-create></baseline-policy-create>
    <baseline-policy-domain-ajax></baseline-policy-domain-ajax>
  </template>

  <script>

    class BaselinePoliciesTab extends Polymer.Element {
      static get is() {
        return 'baseline-policies-tab';
      }

      static get properties() {
      }
    }
    customElements.define(BaselinePoliciesTab.is, BaselinePoliciesTab);

  </script>
</dom-module>
在我的基线策略创建元素中,我有一个下拉按钮,允许我选择包或订阅。当我选择其中一个时,我将发送一个CustomEvent,它将触发我在基线策略域ajax中注册的侦听器。以下是创建基线策略的代码:

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../shared-styles.html">
<link rel="import" href="../../bower_components/dfw-styles/dfw-styles.html">
<link rel="import" href="../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../bower_components/paper-menu-button/paper-menu-button.html">
<link rel="import" href="../../bower_components/paper-listbox/paper-listbox.html">

<dom-module id="baseline-policy-create">
  <template>
    <style include="dfw-styles">
      :host {
        display: block;
      }
      .top-button{
        float : right;
      }
    </style>

    <div class="outer-buttons">
      <paper-menu-button horizontal-align="right" no-overlap="true" no-animations class="top-button">
        <paper-button slot="dropdown-trigger" class="dropdown-trigger create-button btn-primary">
          <iron-icon icon="menu"></iron-icon>
          <span>Create Baseline Policy</span>
        </paper-button>
        <paper-listbox slot="dropdown-content" class="dropdown-content">
          <template is="dom-repeat" items="{{_domains}}">
            <paper-item on-tap="_getDomainSchema">[[item.label]]</paper-item>
          </template>
        </paper-listbox>
      </paper-menu-button>
    </div>
  </template>
  <script>
    class BaselinePolicyCreate extends Polymer.Element {
      static get is() {
        return 'baseline-policy-create';
      }

      static get properties() {
        return {
          _domains: {
            type: Array,
            value: [{'name':'Package', 'label':'Package'},
                    {'name':'Subscription', 'label':'Subscription'}] //TODO: is it possible to get these values from an API source
          }
        }
      }

      _getDomainSchema(evt) {
        console.info("Get the schema for the following domain:", evt.target.innerText);
        // fire event here to trigger ajax call in baseline-policy-domain-ajax
        this.dispatchEvent(new CustomEvent('domain-ajax',{detail : evt.target.innerText}));
      }
    }
    customElements.define(BaselinePolicyCreate.is, BaselinePolicyCreate);
  </script>
</dom-module>
下面是基线策略域ajax的代码:

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


<dom-module id="baseline-policy-domain-ajax">
  <template>
    <style></style>
    <iron-ajax
      id = "getSchema"
      auto = false
      url="<removed-url-for-confidentiality>"
      params='{"domain" : "Package"}'
      handle-as="json"
      on-response="_handleResponse"
      debounce-duration="300">
      </iron-ajax>
  </template>

  <script>
    class BaselinePolicyDomainAjax extends Polymer.Element {
      static get is() { return 'baseline-policy-domain-ajax'; }

      //static get properties() {  }

      //static get observers() {  }

      connectedCallback(){
        super.connectedCallback();
        console.log("ajax element is attached! Register listeners");
        document.addEventListener('domain-ajax',this._editPage.bind(this),true);
      }

      _handleResponse(event, request) {
        console.log ('Handle Response');
        var response = request.response;
        console.log(response);
      }

      _editPage(evt){
        console.log("Change Page to New Baseline Policy");
        console.log(evt.detail);
      }
    }
    customElements.define(BaselinePolicyDomainAjax.is, BaselinePolicyDomainAjax);
  </script>
</dom-module>
为了保密,我删除了url,但是我已经测试了iron ajax组件,并且能够成功地调用API


日志没有告诉我为什么监听者没有从其他聚合物元素听到我的事件。关于我做错了什么有什么想法吗?

正如Mishu在评论中所说,事件在等级中向上传播。因此,在基线策略选项卡中,您需要捕获该事件并手动将其传递给基线策略域ajax

例如:

<dom-module id="baseline-policies-tab">
<template>
<style include="dfw-styles">
  :host {
    display: block;
  }
</style>
<baseline-policy-create on-domain-ajax="_onDomainAjax"></baseline-policy-create>
<baseline-policy-domain-ajax></baseline-policy-domain-ajax>
</template>

<script>

class BaselinePoliciesTab extends Polymer.Element {
  static get is() {
    return 'baseline-policies-tab';
  }

  static get properties() {
  }

  _onDomainAjax(e) {
    this.shadowRoot.querySelector("baseline-policy-domain-ajax")._editPage(e);
  }
}
customElements.define(BaselinePoliciesTab.is, BaselinePoliciesTab);

</script>
</dom-module>

事件在层次结构中向上移动,所以你无法在兄弟姐妹中捕捉到它们,这与聚合物无关,这是事件通常的工作方式。。这里有一个不同的问题,但答案非常相似:@mishu这很有意义,你有没有任何你认为对教我如何在父母之间传递数据有用的文档?正如您在回复中所说,它需要数据绑定。我是网络开发新手,希望能得到一些指导。