首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web组件:如何从父组件访问子组件实例

Web组件:如何从父组件访问子组件实例
EN

Stack Overflow用户
提问于 2022-03-05 02:01:50
回答 1查看 1.1K关注 0票数 3

编辑--我在这个问题中遗漏了一些细节,但假设这两个元素已经定义了。另外,在实际问题中,DOM树更大,子组件是开槽到父组件中。找到子组件没有问题。

我有两个web组件:ParentChild

我可以在标记中使用如下这些组件:

代码语言:javascript
复制
<parent-element>
  <child-element></child-element>
</parent-element>

Child组件具有我想向父组件公开的逻辑。

代码语言:javascript
复制
class Child extends HTMLElement {
  constructor() {
    ...
  }

  publicMethod() {
    console.log('call me from the parent component');
  }
}

理想情况下,我希望像这样从父方法调用子方法:

代码语言:javascript
复制
class Parent extends HTMLElement {
  constructor() {
    ...
  }

  someTrigger() {
    const child = this.shadowRoot.querySelector('child-element');
    child.publicMethod();
  }
}

但是,这是行不通的,因为child是一个HTMLElement,因为querySelector() API返回的是一个HTMLElement,而不是web组件实例。

有办法以类似的方式获得组件的实例吗?我希望能够遍历Parent组件阴影树来找到特定的组件实例。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-05 11:52:16

小心whenDefined

它告诉您Web组件是定义的,而不是当在DOM中解析时:

只要您将<child-element>保存在lightDOM中,<slot>和shadowDOM就与它们无关。

代码语言:javascript
复制
.as-console-wrapper {max-height:100%!important;top:0;zoom:.88}
.as-console-row:nth-child(n-6) {background:#0057B7!important;color:#FFD500!important}
.as-console-row:nth-child(n+6) {background:#FFD500!important;color:#0057B7!important}
.as-console-row-code{padding:0!important}
代码语言:javascript
复制
<script>
  console.clear();
  class Component extends HTMLElement {
    constructor() {
      super();
      console.log(this.nodeName, "constructor")
    }
    connectedCallback(){
      console.log("connectedCallback", this.nodeName, this.children.length, "children");      
    }
  }
  customElements.define("child-component", class extends Component {
    foo() {
      console.log("Executed <child-element>.foo()");
    }
  });
  customElements.define("parent-component", class extends Component {
    connectedCallback() {
      super.connectedCallback();
      customElements.whenDefined('child-component')
                    .then(() => console.log("Promise resolved, <child-component> IS defined!"));
      setTimeout(() => { // wait till lightDOM is parsed
        console.log("After setTimeout",this.nodeName,"has", this.children.length, "children");
        this.querySelectorAll("child-component").forEach(child => child.foo());
      })
    }
  })
</script>
<parent-component>
  <child-component></child-component>
  <child-component></child-component>
</parent-component>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71358910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档