在 Angular 13 中,我使用 _injectDialogHost() 方法创建一个 _bodyComponent,如下所示:
private _injectDialogHost(): void {
const componentFactory = this._componentFactoryResolver.resolveComponentFactory(this.data?.component);
this._bodyComponent = this._dialogHost?.viewContainerRef?.createComponent(componentFactory, 0, this._createInjector());
this._bodyComponent?.changeDetectorRef?.detectChanges();
}
在 Angular 17 中,我更新了此方法,因为 _componentFactoryResolver 已弃用:
private _injectDialogHost(): void {
this._bodyComponent = this._dialogHost?.viewContainerRef?.createComponent(this.data?.component);
this._bodyComponent?.changeDetectorRef?.detectChanges();
}
在 this.data?.component 中,我使用 InjectionToken 创建注入 DIALOG_DATA
const ALLOY_DIALOG_DATA = new InjectionToken<any>('DialogData');
constructor(
@Inject(DIALOG_DATA) public data: IDialogData,
private elRef: ElementRef,
private renderer: Renderer2,
) {
if (this.data?.dialogRef?.componentInstance?.dialogConfig?.resizable === true) {}}
现在的问题是我的 dialogRef 现在未定义,它存在于 matlegacy 中。
现在 this.data 给了我一个 InjectionToken,而不是一个包含内容和dialogRef 的对象,就像它在早期版本的 Angular 和材质中所提供的那样。
您可以尝试注入MatDialogRef而不是IDialogData。
import { MatDialogRef } from '@angular/material/dialog';
constuctor(private readonly dialogRef: MatDialogRef) {}
此外,您还可以在文档中找到一些额外信息:MatDialogRef