sne*_*a_h 5 node.js express typescript angular6
我正在学习 Angular 6 的 MEAN 堆栈应用程序。在这里,我想在添加新的客户端/汽车/司机/预订或更新新的客户端/汽车/司机/预订后刷新组件。但是添加新值后,显示所有值的组件不会更新或刷新(当时无法看到当前值),但是当我在组件之间路由然后返回到它更新并显示的同一组件时所有的值。
我希望当从对话框表单添加或更新值时,它会在那时显示。
这是add client component
export class AddClientComponent implements OnInit {
clientForm: FormGroup;
constructor(private fb: FormBuilder,
private clientService: ClientService,
private toastr: ToastrService,
private router: Router,
public dialogRef: MatDialogRef < AddClientComponent > ) {
this.clientForm = this.fb.group({
'clientname': new FormControl(""),
'clientphoneno': new FormControl(""),
'clientemail': new FormControl(""),
'clientaddress': new FormControl("")
});
}
ngOnInit() {
this.router.navigate(['/admin/clients']);
}
saveClient(formdata: any) {
let theForm = this.clientForm.value;
this.clientService.addClient(theForm).subscribe(data => {
if (data.success === false) {
this.toastr.error(data.message);
} else {
this.toastr.success(data.message);
this.ngOnInit();
this.dialogRef.close();
}
this.clientForm.reset();
});
}
close() {
this.dialogRef.close();
}
}Run Code Online (Sandbox Code Playgroud)
这里是client component显示附加值的地方
export class ClientsComponent implements OnInit {
public currentClient: IClient;
clients: 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
}
})
}
ngOnInit() {
this.getAllClients();
}
getAllClients() {
this.clientService.getClients().subscribe((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);
});
}
}Run Code Online (Sandbox Code Playgroud)
这里是client service
addClient(oClient) {
let headers = new Headers({
'Content-Type': 'application/json'
});
let options = new RequestOptions({
headers: headers
});
return this._http.post('http://localhost:3000/admin/clients/add', JSON.stringify(oClient), options).pipe(
map((response: Response) => response.json()));
}
getClients() {
let headers = new Headers({
'Content-Type': 'application/json'
});
let options = new RequestOptions({
headers: headers
});
return this._http.get('http://localhost:3000/admin/clients', options).pipe(
map((response: Response) => response.json()));
}
getClient(clientid) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({
headers: headers
});
return this._http.get(`http://localhost:3000/admin/clients/${clientid}`, options).pipe(
map((response: Response) => response.json()));
}
editClient(clientId, oClient) {
let headers = new Headers({
'Content-Type': 'application/json'
});
let options = new RequestOptions({
headers: headers
});
return this._http.put(`http://localhost:3000/admin/clients/edit/${clientId}`, JSON.stringify(oClient), options).pipe(
map((response: Response) => response.json()));
}Run Code Online (Sandbox Code Playgroud)
您仅在的生命周期挂钩getAllClients()期间调用。这意味着它只会在组件初始化时加载客户端(这就是为什么当您导航离开并再次返回时会看到新客户端,因为组件重新初始化并且生命周期钩子再次被触发)。OnInitClientsComponentOnInit
为了解决这个问题,您有两个选择(如果您使用状态管理(例如 ngRx),则有两个以上的选择)。
1)当您添加新客户端并关闭对话框时,立即getAllClients()通过订阅afterClosed事件再次调用MatDialog
this.addClientDialogRef.afterClosed().subscribe(() => { this.getAllClients(); } );
Run Code Online (Sandbox Code Playgroud)
或者
2)通过对话框本身的事件提供新的客户端对象afterClosed,并将新对象推送到您的clients数组中。唯一需要注意的是,您需要从 HTTP 请求中返回新的客户端对象saveClient
添加客户端组件
saveClient(formdata: any) {
let theForm = this.clientForm.value;
this.clientService.addClient(theForm).subscribe(data => {
if (data.success === false) {
this.toastr.error(data.message);
} else {
this.toastr.success(data.message);
this.ngOnInit();
this.dialogRef.close(data.client); // <--- Assuming the new object comes under the 'client' key of your response.
}
this.clientForm.reset();
});
}
Run Code Online (Sandbox Code Playgroud)
客户端组件
addClientDialogRef.afterClosed().subscribe((client: Client) => {
this.clients.push(client);
}
Run Code Online (Sandbox Code Playgroud)
该afterClosed事件(简要)记录在此处:https: //material.angular.io/components/dialog/overview
MatDialogRef 提供打开的对话框的句柄。它可用于关闭对话框并在对话框关闭时接收通知。
Run Code Online (Sandbox Code Playgroud)dialogRef.afterClosed().subscribe(result => { console.log(`Dialog result: ${result}`); // Pizza! }); dialogRef.close('Pizza!');
| 归档时间: |
|
| 查看次数: |
16684 次 |
| 最近记录: |