我有这个基本的应用程序,我可以在其中使用数据服务从 Firestore 中获取数据。我有几个页面(组件),球队,球员,统计......
例如我的团队组件:
import { Component, OnInit } from '@angular/core';
import { TeamsService } from '../../services/teams.service';
import { Team } from '../../models/team';
import {Observable} from 'rxjs/Observable';
@Component({
selector: 'app-teams',
templateUrl: './teams.component.html',
styleUrls: ['./teams.component.css']
})
export class TeamsComponent implements OnInit {
public teams: Team[];
editState: boolean = false;
teamToEdit: Team;
showAdd: boolean = false;
showSelect: boolean = false;
selectedTeam: Observable<Team>;
constructor(public teamsService: TeamsService) {
}
ngOnInit() {
this.teamsService.getTeams().subscribe(teams => {
this.teams = teams;
console.log('ngOnInit invoked');
});
}
deleteTeam(event, team) {
const response = confirm('are you sure you want to delete?');
if (response) {
this.teamsService.deleteTeam(team);
}
return;
}
editTeam(event, team) {
this.editState = !this.editState;
this.teamToEdit = team;
}
updateTeam(team) {
this.teamsService.updateTeam(team);
this.teamToEdit = null;
this.editState = false;
}
showAddForm() {
this.showAdd = !this.showAdd;
}
getTeam(event, team) {
this.showSelect = !this.showSelect;
this.selectedTeam = this.teamsService.getTeamById(team.id);
}
}
在 ngOnInit 中,我将数据加载到局部变量中,但是一旦我导航到另一个页面然后返回,数据就消失了。我确实需要刷新页面以重新加载数据。
我应该如何解决这个问题?