简体   繁体   English

Polymer 2.0如何创建元素,然后使用Polymer.importHref导入并使用setAttribute传递数据

[英]Polymer 2.0 How to createElement then import with Polymer.importHref and pass data with setAttribute

I am creating an element, importing it and setting attribute with data. 我正在创建一个元素,将其导入并使用数据设置属性。 I want the code to execute only if the element hasn't been imported and created before. 我希望仅在元素尚未导入和创建之前执行代码。 When the function is called I get the following error: 调用该函数时,出现以下错误:

this._dialogPopUp.open is not a function

What is happening is that this._dialogPopUp.open(); 发生的是this._dialogPopUp.open(); is executing before the element is created and imported. 在创建和导入元素之前执行。 If I hit the button for the second time the pop-up works because it has been created and imported before. 如果我第二次点击该按钮,则弹出窗口将起作用,因为它是之前创建和导入的。 How can I make it wait for the element to be created and imported and then continue the execution? 如何使其等待创建和导入元素,然后继续执行?

Currently I have this implementation: 目前,我有此实现:

_loadDialogPopUp(e) {
  let me = this;
  if(!me._dialogPopUp){
    me._dialogPopUp = document.createElement('su-dialog');
    Polymer.importHref(this.resolveUrl('su-dialog.html'), (e) => {
      this.root.appendChild(this._dialogPopUp);
    });
  }
  customElements.whenDefined('su-dialog').then(() => {
    me._dialogPopUp.open();
    me._dialogPopUp.setAttribute('uid', this.user.uid);
  })
}

you will have to check if the element is actually "upgraded" before you can use it's functions. 您必须先检查该元素是否实际上已“升级”,然后才能使用其功能。 Luckily the standard thought of that. 幸运的是,这是标准思想。

customElements.whenDefined('su-dialog').then(() => {
  this._dialogPopUp.open();
  this._dialogPopUp.setAttribute('uid', this.user.uid);
});

for more Details see https://developers.google.com/web/fundamentals/architecture/building-components/customelements 有关更多详细信息,请参见https://developers.google.com/web/fundamentals/architecture/building-components/customelements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM