我想从app.component.html一部分的导航栏刷新我的卡片集,所以我准备了refresh()函数。
当它被调用时,它会更新变量 Cards 但不会在mainView.html的 html 元素上的ngFor 中呈现它。
如果我从mainView.html 中的html 元素调用(如(click)="loadCards()"),但如果在app.component 中完成相同的((click)="refresh()"),它会呈现更新的集合。 HTML。
export class MainView implements OnInit {
constructor(private mMainController: MainController) {}
Cards: any = [];
ngOnInit() {
this.loadCards();
}
loadCards() {
this.mMainController.getAllCards().subscribe(
(data) => {this.Cards = data); },
(error) => {},
() => {console.log(this.Cards));
}
...
}
export class AppComponent {
...
constructor(private router: Router, private mMainView: MainView) {}
refresh(){
console.log('done');
this.mMainView.loadCards();
}
...
}
更新
尝试使用 @Input() 但无法正常工作。我按照接受的答案中的说明实现了 RefreshService,现在我可以从其他组件刷新内容。
谢谢大家的快速回复。
翻过高山走不出你
慕工程0101907
守着星空守着你
相关分类