相对和绝对导航
导航可以是相对的,也可以是绝对的。
相对导航将单个URL段替换为另一个URL段,或附加您在应用程序中指定的路径
routerLink
例子:
说出你的电流
url
是
localhost:4200/child/1/2/3
<a [routerLink]="['grandchild',4,5]">Go</a>
currentpath+'/grandchild/4/5'
---&燃气轮机;
localhost:4200/child/1/2/3/grandchild/4/5
<a [routerLink]="['/grandchild',4,5]">Go</a>
如果路径以/开头,则它是绝对导航。
这将导致
当前路径+'/孙子/4/5'
localhost:4200/grandchild/4/5
使用导航到路线时
Router.navigate()
ActivatedRoute
使用
relativeTo
财产因为
navigate()
方法不知道当前路由。
使用导航到路线时
RouterLink
激活路线
激活路线
解决方案1:
路由器链接
指令
在你的子组件.html
<a [routerLink]="['grandchild']">Grandchild Without Param</a><br>
在你的子组件.ts
constructor(private router:Router,private route:ActivatedRoute) { }
ngOnInit() {
}
onNaviagte()
{
this.router.navigate(['grandchild'],{relativeTo:this.route});
}
Live Demo