先看路由表
export const parentRoutes:RouterConfig=[
{path:'',redirectTo:'parent',pathMatch:'full'},
{path:'parent',component:parent,
children:[
{path:'home',component:home},
{path:'child',component:child},
{path:'',redirectTo:'home',pathMatch:'full'}
]
}
]
絕對路徑
絕對路徑是以“/”開頭,,需要使用完整路由并與路由表一緻如:
<a [routerLink]="['/parent/child']">link to child</a>
指令行:this.router.navigate(['/parent/child'])
相對路徑
相對路徑以“../”(上級目錄),或“./”開頭(目前目錄),是指相對于目前元件的
<a [routerLink]="['../child']">link to child</a>
指令行:constructor(private router:Router, private route:ActivatedRoute) {
}
onClick() {
this.router.navigate(['./child'], {relativeTo: this.route});
}
注意這裡使用了ActivatedRoute,指目前相對于目前的路由