简体   繁体   English

如何在 angular 组件中定义完整选项卡?

[英]How can I define a full tab in an angular component?

I want to split a large angular component containing a <mat-tab-group> and several <mat-tab> into:我想将包含一个<mat-tab-group>和几个<mat-tab>的大型 angular 组件拆分为:

  • a parent component defining the <mat-tab-group>定义<mat-tab-group>的父组件
  • children components each defining their own <mat-tab>每个子组件都定义自己的<mat-tab>

The template for the parent looks like:父级的模板如下所示:

<mat-tab-group>
  <child-component1></child-component1>
  <child-component2></child-component2>
  <child-component3></child-component3>
</mat-tab-group>

And each child looks like:每个孩子看起来像:

<mat-tab>
  ...
</mat-tab>

The children are properly initialized without errors, however they are not rendered to the DOM.子项已正确初始化且没有错误,但是它们未呈现给 DOM。 It works as expected if I put every <mat-tab> in the parent, and have only the tab content in the children components.如果我将每个<mat-tab>都放在父级中,并且子组件中只有选项卡内容,它会按预期工作。

This is not a proper solution to my problem though: given that the children components need to render the tab label, it doesn't seem right to let the parent responsible of that.但这不是我的问题的正确解决方案:鉴于子组件需要呈现选项卡 label,让父组件对此负责似乎是不对的。

My understanding is that a <mat-tab> element must be a direct child of the <mat-tab-group> to be properly rendered and that my <child-component> s prevent that.我的理解是<mat-tab>元素必须是<mat-tab-group>的直接子元素才能正确呈现,而我的<child-component>阻止了这种情况。

What would be the best way to have the <mat-tab> defined in the children components?在子组件中定义<mat-tab>的最佳方法是什么?

You can make tab list base on your ViewModel and then do like this:您可以根据您的 ViewModel 创建选项卡列表,然后执行以下操作:

 <mat-tab-group [(selectedIndex)]="tabIndex">
    <mat-tab *ngFor="let tab of tabs; let index = index" [label]="tab.Title">
      <ng-template mat-tab-label>
        <span> {{tab.Title}}</span>
      </ng-template>
      <dynamic-content [TabComponent]="tab"></dynamic-content>
    </mat-tab>
  </mat-tab-group>

Then call your component base on Tab Item click, In fact use dynamic component.然后根据Tab Item点击调用你的组件,实际上使用动态组件。

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

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