在 angular2 中调用 api 后重定向到路由 [英] redirect to a route after api call in angular2
本文介绍了在 angular2 中调用 api 后重定向到路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在处理(通过 api)用户通过表单提交的数据后,我需要将用户重定向到不同的组件.下面是我试过的代码.
I need to redirect the user to a different component after processing (via api) the data user submitted through form. Below is the code I tried.
在组件中
onSubmit(model){
if(model.valid === true) {
this.SharedService.postFormdata(model).subscribe(
data => { console.log(data)},
error => Observable.throw(error);
);
this.router.navigate(['PublicPage']);
}
}
但是,调用完成后,它不会重定向到 PublicPage
组件.请任何帮助.谢谢.
However, after call completed, its not redirecting to PublicPage
component. Any help please. Thanks.
推荐答案
import { Router } from '@angular/router';
export class HeroesComponent {
constructor(private router: Router) { }
onSubmit(modal): void {
if(model.valid === true) {
this.SharedService.postFormdata(model).subscribe(
(data) => {
// Page redirect when getting response
this.router.navigate(['/PublicPage']);
},
(error) => {
console.log("err", error);
});
}
}
}
为了更好地理解,请阅读此文档:Angular2 路由
For better understanding read this docs: Angular2 routing
这篇关于在 angular2 中调用 api 后重定向到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文