[英]dialogRef.close() is not a function opening from one component and closing from another component
I have below MatDialog我有下面的 MatDialog
export class ImportProcessingDialog extends AppComponentBase {
constructor(public dialogRef: MatDialogRef<ImportProcessingDialog>) {
super(injector);
}
onCloseClick(): void{
this.dialogRef.close();
}
}
Now I am opening the MatDialog
from one component as below现在我从一个组件打开
MatDialog
,如下所示
export class ImportDataComponent implements OnInit {
constructor(private importProcesingDialog: MatDialog) {}
private onClickImport() {
this.importProcesingDialog.open(ImportProcessingDialog, {
width: '30%'
});
}
}
Now I want to close the Dialog from Another component现在我想从另一个组件关闭对话框
export class RisksComponent {
constructor(public dialogRef: MatDialogRef<ImportProcessingDialog>) {
}
oncloseClick(){
this.dialogRef.close();
}
}
When I do this, getting an exception as当我这样做时,出现异常
NullInjectorError: R3InjectorError(MainModule)[MatDialogRef -> MatDialogRef -> MatDialogRef -> MatDialogRef]:
NullInjectorError: No provider for MatDialogRef!
So added the provider as below所以添加了提供者如下
providers: [{
provide: MatDialogRef,
useValue: {}
},]
With the provider code the error fixed however on I click on oncloseClick
function getting below error使用提供程序代码修复了错误,但是在我单击
oncloseClick
function 时出现以下错误
TypeError: this.dialogRef.close is not a function
How about storing the dialog ref in a service, which can be accessed from any component!如何将对话框 ref 存储在服务中,可以从任何组件访问它!
export class ImportProcessingDialog extends AppComponentBase {
constructor(public dialogRef: MatDialogRef<ImportProcessingDialog>,
private testService: TestService) {
this.testService.dialogRef = dialogRef;
super(injector);
}
onCloseClick(): void{
this.dialogRef.close();
}
}
Then on the risks component you can do然后在风险部分你可以做
export class RisksComponent {
constructor(public dialogRef: MatDialogRef<ImportProcessingDialog>,
private testService: TestService) {
}
oncloseClick(){
if(this.testService.dialogRef) {
this.testService.dialogRef.close();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.