如何在模板中正确解析子组件。

发布于 2025-02-06 01:05:21 字数 1275 浏览 3 评论 0原文

我正在尝试使用Scencen.js来创建具有以下结构的选项卡组件:

<custom-tabs>
    <custom-tab active label="Label 1">
        Some generic HTML content <custom-rating value="4"></custom-rating>
    </custom-tab>
    <custom-tab active label="Label 2">
        Some more generic HTML content <custom-rating value="5"></custom-rating>
    </custom-tab>
</custom-tabs>

父组件需要通过解析每个“自定义标签”组件(没有渲染方法)来渲染基础HTML并提取'Label '来自每个选项卡的参数,构建这些标签的导航列表,并类似地提取每个选项卡的主要内容并显示在选项卡的相应面板中。

在我的父组件中,我在“ componentwillload”钩中获取选项卡

componentWillLoad() {
    // Grab tabs from this component
    this.tabs = Array.from(this.el.querySelectorAll('custom-tab'));
    console.log(this.tabs);
  }

,然后在渲染函数中我尝试输出内容:

return (
      <div>
        {this.tabs.map((tab: HTMLCustomTabElement, index: number) => {
          return (
            <div
              role="tab"
              onClick={() => this.openTab(index)}>
              {tab.label} {tab.innerHTML}
            </div>
          );
        })}
      </div>
    );

问题是tab.innerhtml没有解析自定义评分< /code>组件,然后将其作为文本输出。您能否提出正确显示标签内容的正确方法,以使所有内容都正确解析?

I am trying to use Stencil.js to create tabs component with the following structure:

<custom-tabs>
    <custom-tab active label="Label 1">
        Some generic HTML content <custom-rating value="4"></custom-rating>
    </custom-tab>
    <custom-tab active label="Label 2">
        Some more generic HTML content <custom-rating value="5"></custom-rating>
    </custom-tab>
</custom-tabs>

The parent component needs to render the underlying HTML by parsing each 'custom-tab' component (which doesn't have the render method) and extracting the 'label' parameter from each tab, building the navigation list of those labels and similarly extracting the main content of the each tab and displaying in in the corresponding panel of the tab.

In my parent component I grab the tabs in the 'componentWillLoad' hook

componentWillLoad() {
    // Grab tabs from this component
    this.tabs = Array.from(this.el.querySelectorAll('custom-tab'));
    console.log(this.tabs);
  }

and then in render function I try to output the contents:

return (
      <div>
        {this.tabs.map((tab: HTMLCustomTabElement, index: number) => {
          return (
            <div
              role="tab"
              onClick={() => this.openTab(index)}>
              {tab.label} {tab.innerHTML}
            </div>
          );
        })}
      </div>
    );

The problem is tab.innerHTML is not parsing the custom-rating component and just outputs it as text. Could you please suggest the right way to display the tab's content so that everything is parsed properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱情眠于流年 2025-02-13 01:05:21

您可以通过设置 innerhtml

return (
  <div>
    {this.tabs.map((tab: HTMLCustomTabElement, index: number) => {
      return (
        <div
          role="tab"
          onClick={() => this.openTab(index)}>
          {tab.label}
          <div innerHTML={tab.innerHTML}></div>
        </div>
      );
    })}
  </div>
);

You can output HTML by setting the innerHTML property:

return (
  <div>
    {this.tabs.map((tab: HTMLCustomTabElement, index: number) => {
      return (
        <div
          role="tab"
          onClick={() => this.openTab(index)}>
          {tab.label}
          <div innerHTML={tab.innerHTML}></div>
        </div>
      );
    })}
  </div>
);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文