当子组件调用方法时,angular.io中父组件的Propos不会改变。
我有这个方法dashboard.component:
listPeers(){
this.loading = true;
this.localStorage.getItem("payload").subscribe( (storage : string) =>{
this.jwt = storage;
this.userService.listPeers(this.jwt).subscribe( (res) => {
this.loading = false;
if(res.success){
console.log(res.peers)
this.peers = res.peers;
this.listUsersCrm();
}else{
this.snackBar.openFromComponent(ToastComponent, {
data: res.msg
});
this.router.navigate(['/notfound']);
}
})
})
}
在“commit-changes.component”上,我调用了这个方法:
import { Component, OnInit} from '@angular/core';
import { DashboardComponent } from "../dashboard/dashboard.component";
@Component({
providers: [DashboardComponent],
selector: 'app-commit-changes',
templateUrl: './commit-changes.component.html',
styleUrls: ['./commit-changes.component.scss']
})
export class CommitChangesComponent implements OnInit {
constructor(private dashBoardComponent : DashboardComponent) { }
ngOnInit() {
}
discardChanges(){
this.dashBoardComponent.listPeers();
}
}
此方法已被调用,但来自父级 (dashboard.component) 的道具没有更改。
注意:该方法listPeers是调用 API 列出存储在 BD 上的 peers 并设置 peers props。
this.peers = res.peers;
执行此方法并应用道具更改的正确方法是什么?