我已经阅读了几篇答案并尝试了许多方法,但似乎无法使其正常工作。在我的Angular 6应用程序中,我尝试打开MatDialog,但是我收到以下错误:
ERROR Error:
ExpressionChangedAfterItHasBeenCheckedError:
表达式在检查后已发生更改。之前的值:'@slideDialog: enter'。当前值:'@slideDialog: exit'。
当我调试应用程序并单击结账按钮时,MatDialog会打开,然后在执行几行代码后关闭。在不使用断点运行应用程序时,您根本看不到对话框打开。
这是模板文件:
<div>
<button id="cancelButton" mat-raised-button (click)="clearCart()">Cancel</button>
<button id="checkoutButton" mat-raised-button color="primary" (click)="openCheckoutDialog(dataSource)" [disabled]="isTableEmpty()">Checkout</button>
</div>
当我点击ID为“checkoutButton”的按钮时,它调用openCheckoutDialog()
。
这是调用openCheckoutDialog的我的类:
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-shopping-cart',
templateUrl: './shopping-cart.component.html',
styleUrls: ['./shopping-cart.component.css']
})
export class ShoppingCartComponent implements OnInit {
dataSource: MatTableDataSource<Item>;
itemList: Item[] = [];
constructor(public dialog: MatDialog, private cdr: ChangeDetectorRef) { }
openCheckoutDialog(): void {
const dialogRef = this.dialog.open(ReviewItemListComponent, {
width: '250px',
data: this.itemList
});
// this.cdr.detectChanges();
dialogRef.afterClosed().subscribe(result => {
console.log(`Dialog result: ${result}`);
});
// this.cdr.detectChanges();
}
}
我已经在上述两个地方尝试使用this.cdr.detectChanges()
,但我仍然收到错误信息。
这是我的ReviewItemListComponent模板(当结账按钮被点击时弹出对话框):
<div>
<h1 mat-dialog-title>
Are you sure?
</h1>
<mat-dialog-actions>
<button tabindex="-1" mat-raised-button [mat-dialog-close]="true">Cancel</button>
<button tabindex="-1" mat-raised-button color="primary" (click)="confirm()">Confirm</button>
</mat-dialog-actions>
</div>
这里是ReviewItemListComponent类:
import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { Item } from '../item';
@Component({
selector: 'app-review-item-list',
templateUrl: './review-item-list.component.html',
styleUrls: ['./review-item-list.component.css']
})
export class ReviewItemListComponent implements OnInit {
constructor(public dialogRef: MatDialogRef<ReviewItemListComponent>,
@Inject(MAT_DIALOG_DATA) public data: Item[]) { }
ngOnInit() {
this.dialogRef.close();
}
confirm(): void {
this.dialogRef.close();
}
}
我没有对数据做任何更改,这也是大多数问题和答案所涉及的内容,那么我该如何解决打开MatDialog的问题?
编辑:
事实证明,我把this.dialogRef.close()
粘贴到了错误的方法中。在这种情况下,我把它放在了ngOnInit()
中,当我创建我的review-item-list-component.ts文件时。所以一旦打开它,我就关闭它。这仍然无法解释错误。关闭最近打开的对话框在ngOnInit()
内有什么问题?
ngOnInit
中关闭对话框本身并没有什么本质的问题。问题在于视图(对话框)在变更检测周期中被修改了。(在开发模式下会运行两次 - 在生产环境中不会出现此错误)如果你将dialog.close()
移动到ngAfterViewInit
中,我相信它应该可以避免抛出该错误。 - Michael Doye