Angular 如何导航到家?
我无法找到回家的路线。 当我这样做时,它是在html中工作的:Angular 如何导航到家?,angular,Angular,我无法找到回家的路线。 当我这样做时,它是在html中工作的: <a href="/"><i class="icon-power"></i></a></li> 我需要在导航前打电话给服务部。但这是行不通的。有什么建议可以解决这个问题吗?您可以这样做: import { Router } from '@angular/router'; constructor(private router: Router){} logout(){
<a href="/"><i class="icon-power"></i></a></li>
我需要在导航前打电话给服务部。但这是行不通的。有什么建议可以解决这个问题吗?您可以这样做:
import { Router } from '@angular/router';
constructor(private router: Router){}
logout(){
this.api.logout('/logout-user').subscribe((r:any)=>{
// after the api call user will be redirected to home page
this.router.navigate(['/']);
// OR
this.router.navigate(['']);
});
}
如果Vivek Doshi的答案不能解决您的问题,那么您应该检查是否将路径“”重定向到“home” 路线示例:
const routes : Routes = [
{ path: "home", component: HomeComponent },
{ path: "dashboard", component: DashboardComponent },
{ path: "", redirectTo: "/home", pathMatch: "full" }
];
您可以将导航放在subscribe blockArgument中,类型为“/”的参数不可分配给类型为“any[]”的参数。我已经尝试过了…在我的问题中…我不关心服务中的响应如果这个答案对您不起作用,您可能在服务调用和下一个()中实现了一些难以置信的错误从不调用->导航从不调用。foreget about service router.navigate['/')不会重定向到主页page@None,请检查更新的答案并确认这些答案与minw相同。
const routes : Routes = [
{ path: "home", component: HomeComponent },
{ path: "dashboard", component: DashboardComponent },
{ path: "", redirectTo: "/home", pathMatch: "full" }
];