5

我想在这里实现的是我想在我的共享组件中包装有角度的材料标签组件。

所以,这是我要包装的组件:

PS:我可以在每个选项卡中显示组件:

<mat-tab-group>
  <mat-tab label="First"> Content 1 </mat-tab>
  <mat-tab label="Second"> Content 2 </mat-tab>
  <mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>

所以,我想使用包装器来使用它:

<app-wrapper>
  <app-item>
    <app-item-header title="first"></app-item-header>
    <app-item-content>
      <app-needs-to-be-displayed></app-needs-to-be-displayed>
    </app-item-content>
  </app-item>
</app-wrapper>

应用程序包装器.html

<mat-tab-group>
  <ng-content></ng-content>
</mat-tab-group>

TS类没有变化

应用-item.html

<mat-tab>
    <ng-content></ng-content>
</mat-tab>

TS类没有变化

app-item-header.html

<ng-template mat-tab-label>
  {{title}}
</ng-template>

app-item-header.ts 类

@Input() title:string = ''

app-item-content.html

<div>
   <ng-content></ng-content>
</div>

TS 类没有变化,这可以容纳一个实际的组件

这在控制台中没有错误,但页面上也没有出现任何内容。

这是工作的stackblitz版本

https://stackblitz.com/edit/angular-wrapping-component-with-ng-content

PS:如果这是实现这一目标的最佳解决方案还是有另一种解决方案,请建议大家?

4

1 回答 1

3

您看不到任何内容,因为mat-tab组件需要是组件的直接子级,mat-tab-group否则它不知道存在任何选项卡。因此,您需要以不同的方式处理事情。您需要从自定义组件中获取内容,然后在包装器组件中呈现所有内容。我认为最简单的方法是将内容作为模板获取,它也非常接近您创建的内容。我将从最低的组件开始并向上移动。

app-item-content组件可以是您创建它的样子。app-item-header需要改变。与选项卡一样,该mat-tab-label指令也需要直接在mat-tab-group. 这就是我从模板中删除它的原因。

<ng-template>
  {{title}}
</ng-template>

主要变化是我使用ViewChildng-template装饰器将组件内部也作为代码中的属性公开。这也允许从组件外部访问模板。

@Component({
  selector: 'app-item-header',
  templateUrl: './item-header.component.html',
  styleUrls: ['./item-header.component.css']
})
export class ItemHeaderComponent implements OnInit {
  @ViewChild(TemplateRef) public headerTemplate: TemplateRef<any>;
  @Input() title:string = ''
}

之后就可以移动到app-item组件了。这里也只是模板的一个小改动。使用与标题组件中相同的方法,您可以获得项目内容的模板。

<ng-template>
    <ng-content select="app-item-content"t></ng-content>
<ng-template>

在代码中,您可以像以前一样再次使用ViewChild装饰器来获取内容模板。标题是项目的子组件,您需要使用不同但相似的ContentChild装饰器来访问提供标题模板的标题组件。

@Component({
  selector: 'app-item',
  templateUrl: './item.component.html',
  styleUrls: ['./item.component.css']
})
export class ItemComponent implements OnInit {
  @ViewChild(TemplateRef) public contentTemplate: TemplateRef<any>;
  @ContentChild(ItemHeaderComponent) public itemHeader: ItemHeaderComponent;
}

此时,您可以访问在mat-tab组件内呈现内容所需的一切。在模板中,您需要呈现与之前公开的模板中的每个组件和内容相对应的mat-tab-group和 a 。mat-tabapp-item

<mat-tab-group>
  <mat-tab *ngFor="let item of appItems">
    <ng-template mat-tab-label>
    <ng-container *ngTemplateOutlet="item.itemHeader.headerTemplate"></ng-container>
    </ng-template>
    <ng-container *ngTemplateOutlet="item.contentTemplate"></ng-container>
  </mat-tab>
</mat-tab-group>

正如您所看到的,您为每个项目创建一个选项卡并在项目的内容中呈现非常简单。有点棘手的是您需要在另一个模板中呈现模板的标题,ng-template以便能够添加mat-tab-label被识别为标题内容的指令。要获取app-item包装器组件中的所有组件,请使用ContentChildren装饰器,它提供内容内的组件列表。就像前面的例子一样,我们只是在代码中添加一个属性,其余的由 Angular 处理。

@Component({
  selector: 'app-wrapper',
  templateUrl: './wrapper.component.html',
  styleUrls: ['./wrapper.component.css']
})
export class WrapperComponent implements OnInit {
  @ContentChildren(ItemComponent) public appItems: QueryList<ItemComponent>;
}

我还为你创建了StackBlitz 的一个分支,你可以看到它在工作。

于 2021-06-30T23:35:48.520 回答