Angular 发布或更新数据后,导航到主页,不获取最后的数据
当我在另一个组件中创建或更新数据时。这个首页抓取数据不同步,只是我要点击一个导航链接,然后正确抓取所有数据 以及在更新或创建后导航到主页而不刷新数据 (正确地说,在更新后,所有内容都正常工作,要查看最后的数据,我必须单击页面的一个链接) 我做错了什么 我很感激如果有人帮我,这花了我所有的时间Angular 发布或更新数据后,导航到主页,不获取最后的数据,angular,Angular,当我在另一个组件中创建或更新数据时。这个首页抓取数据不同步,只是我要点击一个导航链接,然后正确抓取所有数据 以及在更新或创建后导航到主页而不刷新数据 (正确地说,在更新后,所有内容都正常工作,要查看最后的数据,我必须单击页面的一个链接) 我做错了什么 我很感激如果有人帮我,这花了我所有的时间 constructor(private pokedexApi:ApiPokemonsService) { } ngOnInit(): void { this.getPokemons() } //
constructor(private pokedexApi:ApiPokemonsService) {
}
ngOnInit(): void {
this.getPokemons()
}
// instead of pokemon having an Id of the pokedex, is better to retrieve the data of that id to the pokemon attribute
getPokemons() {
// subscribing Api data from the this method
this.pokedexApi.getPokemons().subscribe(data => {
// this.isLoading = true
this.data = data;
// console.log("from the details")
// instead of
for (let pokemon of this.data) {
// this.isLoading = true
let id = pokemon.pokedexData;
// console.log(typeof(id))
this.pokedexApi.getPokedex(Number(id)).subscribe(data => {
// this.isLoading = false
pokemon.pokedexData = data;
});
}
},
error => {
console.log(error);
})
}
}
您需要等待
addPokemon
方法完成:
将重定向移动到订阅:
this.apiPokemons.addPokemon(this.user).subscribe(data=> {
this.toast.open('successfully added new Pokemon');
});
this.router.navigate([`${'/pokemons'}`])
谢谢工作,所以每次我都要等到订阅完成。如果我在这之后创建了一个方法,那该怎么办?ddPokemon`我必须等待直到完成下标,然后你可以使用pipe()和map操作符,或者forkJoin()合并多个观测值。一个例子:
this.apiPokemons.addPokemon(this.user).subscribe(data=>{
this.toast.open('successfully added new Pokemon');
this.router.navigateByUrl('/pokemons');
});