Javascript 如何根据回复指向页面6

Javascript 如何根据回复指向页面6,javascript,angular,router,Javascript,Angular,Router,我的UI上有两个页面,我想根据HTTP响应重定向用户。PFB代码 { "routerName": this.routerName, "serviceId" : this.serviceId, "serviceType" : ILL } 现在我的服务类型是ILL,然后我想将页面指向ILL页面,当值服务类型是GVPN时,我想将其指向GVPN页面。 PFB路由模块 RouterModule.forRoot([ { path: 'ill' , comp

我的UI上有两个页面,我想根据HTTP响应重定向用户。PFB代码

 {
      "routerName": this.routerName,
      "serviceId" : this.serviceId,
      "serviceType" : ILL
}
现在我的服务类型是ILL,然后我想将页面指向ILL页面,当值服务类型是GVPN时,我想将其指向GVPN页面。 PFB路由模块

RouterModule.forRoot([
      { path: 'ill' , component: IpIllComponent }, // Want to go to this page when service type is ILL
      { path: 'gvpn' , component: IpGvpnComponent } // Want to go to this page when service type is GVPN
    ])

请在这方面帮助我。

您需要在类中插入路由器,然后使用它导航:

constructor(private router: Router) {}

const path = response.serviceType === 'ILL'? 'ill' : 'gvpn';
this.router.navigate([path]) 
请参阅文档:

构造函数(专用路由器:路由器,专用http:HttpClient){}
RouteByServiceResponse(){
this.http.get().subscribe(响应=>
如果(response.serviceType=='ILL')
{
this.router.navigateByUrl('/ill');
}else if(response.serviceType==='GVPN'){
this.router.navigateByUrl('/gvpn');
}
});
}

在运行时,您可以检查服务类型,然后使用
路由器。导航
以导航到所需的页面

if (serviceType === 'ILL')
  this.router.navigate(['/ill'])
else
  this.router.navigate(['/gvpn']);
请遵循此文档。您将发现所有与角度布线相关的问题


从您的服务获得响应后,您可以使用this.router.navigate(['/${res.serviceType}'])方法重定向用户;别忘了从@angular/RouterLink导入路由器。从响应中获取路径值后,可以根据RouterLink和button进行导航吗?是的,我尝试了RouterLink=“{{path}}”。工作顺利,谢谢。
if (serviceType === 'ILL')
  this.router.navigate(['/ill'])
else
  this.router.navigate(['/gvpn']);
constructor(private router: Router) {}

this.router.navigate(['/gvpn']);