将 Angular 材质对话框迁移到 17 并弃用 Angular 中的组件工厂

问题描述 投票:0回答:1

在 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 和材质中所提供的那样。

我如何才能再次获得该dialogRef,以便我的代码能够像以前一样工作? SS参考如下:

angular angular-material angular17 material-dialog injection-tokens
1个回答
0
投票

您可以尝试注入MatDialogRef而不是IDialogData。

import { MatDialogRef } from '@angular/material/dialog';
    
constuctor(private readonly dialogRef: MatDialogRef) {}

此外,您还可以在文档中找到一些额外信息:MatDialogRef

© www.soinside.com 2019 - 2024. All rights reserved.