Injector
代码在到达这一行之前运行良好:
const componentPortal = new ComponentPortal(ConfirmDialogComponent, null, customInjector);
在终端和浏览器控制台中产生以下错误:
在循环依赖关系中检测到警告:
src\app\确认-dialog.service.ts->src\app\confirm对话框\confirm-dialog.component.ts->src\app\确认-dialog.service.ts
在循环依赖关系中检测到警告:
src\app\confirm对话框\confirm-dialog.component.ts->src\app\确认-dialog.service.ts->src\app\confirm对话框\confirm-dialog.component.ts
并在浏览器控制台中产生以下错误:
未捕获错误:无法解析ConfirmDialogComponent:(?)的所有参数。
@Injectable({
providedIn: 'root'
})
export class ConfirmDialogService {
constructor(
private overlay: Overlay,
private injector: Injector,
) { }
public open() {
const overlayRef = this.overlay.create();
const dialogRef = new CustomOverlayRef(overlayRef);
const customInjector = this.createInjector(dialogRef);
const componentPortal = new ComponentPortal(ConfirmDialogComponent, null, customInjector);
const componentRef = overlayRef.attach(componentPortal);
}
private createInjector(customOverlayRef: CustomOverlayRef) {
const injectionTokens = new WeakMap();
injectionTokens.set(CustomOverlayRef, customOverlayRef);
return new PortalInjector(this.injector, injectionTokens);
}
}
export class CustomOverlayRef {
constructor(
private overlayRef: OverlayRef,
) { }
public close() {
this.overlayRef.dispose();
}
@Component({
selector: 'app-confirm-dialog',
templateUrl: './confirm-dialog.component.html',
styleUrls: ['./confirm-dialog.component.scss']
})
export class ConfirmDialogComponent implements OnInit {
constructor(
private customDialogRef: CustomOverlayRef,
) { }
ngOnInit() {
}
public close() {
this.customDialogRef.close();
}
}