我正在学习一个角度为6的MEAN Stack应用程序。在这里,我想在添加新的客户端/汽车/驾驶员/预订或更新新的客户端/汽车/驾驶员/预订之后刷新组件。但是添加新值后,显示所有值的组件不会更新或刷新(当时无法看到当前值),但是当我在组件之间路由然后返回到同一组件时,它会更新并显示所有值。
我正在使用 afterClosed
的 MATDialog
方法返回所有客户值,但是有错误
错误:无法读取未定义的属性'afterClosed'
如何解决?
这是client ts
文件。
import {Component,OnInit} from '@angular/core';
import {MatDialog,MatDialogRef,MAT_DIALOG_DATA} from '@angular/material/dialog';
import { IClient} from './client';
import {Router,ActivatedRoute} from '@angular/router';
import {ToastrService} from 'ngx-toastr';
import {AddClientComponent} from './add-client/add-client.component';
import {EditClientComponent} from './edit-client/edit-client.component';
import {DialogService} from '../../dialog.service';
import {ClientService} from './client.service';
@Component({
selector: 'app-clients',
templateUrl: './clients.component.html',
styleUrls: ['./clients.component.css']
})
export class ClientsComponent implements OnInit {
public currentClient: IClient;
clients: any;
Client: any;
addClientDialogRef: MatDialogRef < AddClientComponent > ;
editClientDialogRef: MatDialogRef < EditClientComponent > ;
constructor(public dialog: MatDialog,
private dialogService: DialogService,
private clientService: ClientService,
private router: Router) {}
openAddClientDialog() {
this.dialogService.open(AddClientComponent, {
width: '500px'
});
}
openEditClientDialog(id) {
this.dialogService.open(EditClientComponent, {
width: '500px',
data: {
'id': id
}
})
}
closeAll() {
this.addClientDialogRef.afterClosed().subscribe(() => {
this.getAllClients();
});
}
ngOnInit() {
this.closeAll();
this.getAllClients();
}
getAllClients() {
this.clientService.getClients().subscribe((res) => {
console.log(res);
this.clients = res;
}, err => {
console.log(err);
});
}
deleteClient(id) {
this.clientService.deleteClient(id)
.subscribe(res => {
this.router.navigate(['./clients']);
this.ngOnInit();
}, (err) => {
console.log(err);
});
}
}
答案 0 :(得分:0)
要在对话框引用中引用诸如afterClosed()
之类的事件,您需要将open()
的结果保存到相应的引用中:
// ...
@Component({
selector: 'app-clients',
templateUrl: './clients.component.html',
styleUrls: ['./clients.component.css']
})
export class ClientsComponent implements OnInit {
public currentClient: IClient;
clients: any;
Client: any;
addClientDialogRef: MatDialogRef<AddClientComponent>;
editClientDialogRef: MatDialogRef<EditClientComponent>;
constructor(public dialog: MatDialog,
private dialogService: DialogService,
private clientService: ClientService,
private router: Router) { }
openAddClientDialog() {
this.addClientDialogRef = this.dialogService.open(AddClientComponent, {
width: '500px'
});
}
openEditClientDialog(id) {
this.editClientDialogRef = this.dialogService.open(EditClientComponent, {
width: '500px',
data: { 'id': id }
})
}
closeAll() {
this.addClientDialogRef.afterClosed().subscribe(() => {
this.getAllClients();
});
}
// ...
}
希望有帮助!