angular - 在 Electron 中关闭 MatDialogRef
问题描述
我正在开发一个通过 Electron 2.0.5 编译的 Angular 5 应用程序。我 MatDialogRef
用来获取用户输入(Yes
或No
)并使用响应来管理一些业务逻辑。显示后MatDialogRef
,我无法关闭它。
对话框的 HTML 参考:confirm.component.html
<div class="confirm">
<h1 mat-dialog-title>Confirm</h1>
<div mat-dialog-content>
{{dialogData.message}}
</div>
<div mat-dialog-actions>
<button mat-button [mat-dialog-close]="'cancel'" (click)="close()">No</button>
<button mat-button [mat-dialog-close]="'yes'" cdkFocusInitial (click)="close()">Yes</button>
</div>
</div>
逻辑:confirm.component.ts
import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
@Component({
selector: 'app-confirm',
templateUrl: './confirm.component.html',
styleUrls: ['./confirm.component.scss']
})
export class ConfirmComponent implements OnInit {
dialogData: any;
constructor(private dialogRef: MatDialogRef<ConfirmComponent>,
@Inject(MAT_DIALOG_DATA) private data: any) {
this.dialogData = data;
}
ngOnInit() {
}
close() {
this.dialogRef.close();
}
}
最后在我的 app.ts 上:
...
const dialogRef = this.matDialog.open(ConfirmComponent, {
data: {
message: 'Yes or No?'
}
});
return dialogRef.afterClosed()
.switchMap(result => {
if (result !== 'cancel') {
// Do something
} else {
// Do something
}
});
...
通过 VSCode 调试应用程序时,我可以确认 close() 方法正在被击中。对话框没有关闭,也没有控制台错误。如何关闭垫子对话框参考?
解决方案
看来,您的代码缺少订阅:
dialogRef.afterClosed().subscribe(result => {
console.log(`Dialog result: ${result}`);
});
替换switchMap
_subscribe
请参阅此处的文档示例:https ://material.angular.io/components/dialog/overview#dialog-content
顺便说一句,您可以使用此替代方案,而无需mat-dialog-close
指令:
请参阅https://blog.angular-university.io/angular-material-dialog/(第 5 步)
您可以通过以下方式将修改后的表单数据传回 AppComponent:
<div mat-dialog-actions>
<button mat-button (click)="close(false)">No</button>
<button mat-button (click)="close(true)">Yes</button>
</div>
和
close(clickResult: boolean): void {
this.matDialogRef.close(clickResult);
}
您现在可以通过以下方式接收对话数据:
dialogRef.afterClosed().subscribe(
data => console.log("Dialog output:", data)
);
推荐阅读
- docker - 是否可以使用环境变量作为 docker 健康检查
- javascript - GraphQL 查询中的错误,它显示未知类型“ContentfulSizes”
- android - 当我们从显示设置中增加字体大小和显示大小时,文本溢出
- php - 将 Shell Curl 命令转换为 PHP
- angular - 我激活的溃败在 url 中找不到参数。我该如何解决?
- php - 如何从包含的 PHP 文件进行的 load() 调用中更改 div 文本?
- macos - 如何根据需要对目录进行排序
- clang-tidy - clang-tidy:将一些检查列入黑名单(或检查的正则表达式)
- html - 我的页边距有问题。宽度不起作用
- ios - swift - 像 Instagram 应用一样预下载视频