Polymer this.shadowRoot.querySelector(';#upload';)始终返回空值2.x

Polymer this.shadowRoot.querySelector(';#upload';)始终返回空值2.x,polymer,polymer-2.x,dom-if,Polymer,Polymer 2.x,Dom If,访问以下dom if模板中的元素以添加自定义事件侦听器时遇到问题: <template is="dom-if" if=[[!uploaded]]> <vaadin-upload id="upload" target="http://localhost:3000/uploadFile" form-data-name="doc" max-files="1"> <iron-icon slot="drop-label-icon" icon="desc

访问以下
dom if
模板中的元素以添加自定义事件侦听器时遇到问题:

<template is="dom-if" if=[[!uploaded]]>
    <vaadin-upload id="upload" target="http://localhost:3000/uploadFile" form-data-name="doc" max-files="1">
        <iron-icon slot="drop-label-icon" icon="description"></iron-icon>
        <span slot="drop-label">....</span>
    </vaadin-upload>
</template>
<template is="dom-if" if="[[uploaded]]">
      <pdf-element src=[[....]]></pdf-element>
</template> 
默认情况下,
upload
属性为
false
。我在房间里找了找,但没有找到办法让它工作。我总是得到
null

我已经在
dom repeat
模板中毫无问题地使用了
this.shadowRoot.querySelector
。如何使
querySelector
dom if
一起工作?

即使
if
条件为
true
,但
内容尚未压印在
connectedCallback
中,因此您必须等到下一个渲染帧时使用:


@syliaBARAKA没问题:)
connectedCallback() {
    super.connectedCallback();
    console.log(this.shadowRoot.querySelector('#upload'))
    this.shadowRoot.querySelector('#uploadFile').addEventListener('upload-response', event=>this._uploadFile(event))
    this.shadowRoot.querySelector('#uploadFile').addEventListener('upload-success', event=>this._uploadFileSuccessful(event))
  }
Polymer.RenderStatus.afterNextRender(this, () => {
  const uploader = this.shadowRoot.querySelector('#upload');
  uploader.addEventListener('upload-success', () => { /* ... */ });
});