Javascript 打开对话框时出现未捕获类型错误

Javascript 打开对话框时出现未捕获类型错误,javascript,polymer,polymer-2.x,Javascript,Polymer,Polymer 2.x,我在Polymer 2中编码,并试图在函数运行时打开一个纸张对话框组件: _boundListener(e) { this.error = e.detail; console.log(this.error); this.$.dialog.open(); } 我已验证this.error正在运行并且包含正确的数据。我的模板中有一个id为dialog的纸质对话框,但是当此函数运行时,我收到以下消息: 未捕获的TypeError:无法读取未定义的属性“dialog” 有什么想法吗?短暂性

我在Polymer 2中编码,并试图在函数运行时打开一个纸张对话框组件:

_boundListener(e) {
  this.error = e.detail;
  console.log(this.error);
  this.$.dialog.open();
}
我已验证this.error正在运行并且包含正确的数据。我的模板中有一个id为dialog的纸质对话框,但是当此函数运行时,我收到以下消息:

未捕获的TypeError:无法读取未定义的属性“dialog”

有什么想法吗?短暂性脑缺血发作

此函数在触发事件侦听器时运行。此函数将触发该侦听器:

  testError(e) {
    const err = e.detail.request.xhr.response.message;
    window.dispatchEvent(new CustomEvent('_GEtesterror', {bubbles: true, detail: err}));
  }
我不得不使用window.dispatchEvent来触发此事件,因为它不会在对话框所在的html中冒泡。这可能就是它找不到此对话框的原因。$

以下是侦听器函数的外观:

connectedCallback() {
  super.connectedCallback();
  window.addEventListener('_GEtesterror', this._boundListener);
  this._boundListener.bind(this);
}
disconnectedCallback() {
  super.disconnectedCallback();
  window.removeEventListener('error', this._boundListener);
}
_boundListener(e) {
  this.error = e.detail;
  console.log(this, this.$);
  this.$.dialog.open();
}

出现此错误是因为
此。$
未定义

使用
console.log(this,this.$)
确定调用函数的范围。调用侦听器时使用了错误的作用域

另外,您的boundListener绑定到正确的范围:

connectedCallback() {
  super.connectedCallback();
  window.addEventListener('_GEtesterror', this._boundListener.bind(this));
}
disconnectedCallback() {
  super.disconnectedCallback();
  window.removeEventListener('error', this._boundListener.bind(this));
}
_boundListener(e) {
  this.error = e.detail;
  console.log(this, this.$);
  this.$.dialog.open();
}

所以错误是说,
this.$
是未定义的。它似乎在抱怨我在$之后放了什么,因为
$
this
中没有定义<代码>控制台.log(this.$);//未定义的我猜这不是你想象的这。$是未定义的。试着找出如何调用
$
这就是我引用打印“Window{postMessage:ƒ,blur:ƒ,focus:ƒ,close:ƒ,frames:Window,…”的纸质对话框的方式。你能发布更多的代码吗?确定问题的根本原因会很有帮助。此处调用我的侦听器:connectedCallback(){super.connectedCallback();window.addEventListener(“'u GeteStror',this.'u boundListener”);console.log(“connectedCallback Run”);}尝试在connectedCallback函数中添加带有.bind的_boundListener函数,像这样:
this.\u boundListener.bind(this)
。这已经奏效了。谢谢你的帮助,我希望能把它向前支付!