我已经将一个Angular Material 5应用程序移植到Angular Universal上,而且我在使用MatDialog时出现间歇性问题。当它打开时,没有标题或内容。只显示“确定”和“取消”按钮,它们无法正常工作。
我正在使用服务来创建对话框。这是代码。
import { Observable } from 'rxjs/Observable';
import { MatDialogRef, MatDialog, MatDialogConfig } from '@angular/material';
import { Injectable } from '@angular/core';
import { AppConfirmComponent } from './app-confirm.component';
@Injectable()
export class AppConfirmService {
constructor(private dialogRef: MatDialog) { }
public confirm(title: string, message: string, id: string): Observable<boolean> {
const idCancel = id + '-cancel';
let dialogRef: MatDialogRef<AppConfirmComponent>;
dialogRef = this.dialogRef.open(AppConfirmComponent, {
width: '300px',
disableClose: true,
data: {title, message, id, idCancel}
});
return dialogRef.afterClosed();
}
}
组件:
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { Component, Inject } from '@angular/core';
@Component({
selector: 'app-confirm',
template: `<h1 matDialogTitle>{{ data.title }}</h1>
<div mat-dialog-content>{{ data.message }}</div>
<div mat-dialog-actions>
<button id="{{ data.id }}" type="button" mat-raised-button color="primary"
(click)="dialogRef.close(true)">OK</button>
<span fxFlex></span>
<button id="{{ data.idCancel }}" type="button" color="accent" mat-raised-button
(click)="dialogRef.close(false)">Cancel</button>
</div>`,
})
export class AppConfirmComponent {
constructor(
public dialogRef: MatDialogRef<AppConfirmComponent>,
@Inject(MAT_DIALOG_DATA) public data: any
) {}
}