总结一下路径参数和查询参数,为了方便大家对比观察 写在一起方便对比
路径参数的配置--查询参数的配置
{ path: ':tabLink',component: HomeDetailComponent,}
{path: 'home',component: HomeContainerComponent,}
路径参数的跳转--查询参数的跳转
1.<a [routerLink]="['/home',tablink]"></a>
2.this.router.navigate(['home',tablink])
1.<a [routerLink]="['/home']" [queryParams={name:'val1'}]></a>
2.this.router.navigate(['home'],{queryParams:{name:'val1'}});
Url表现形式
http://localhost:4200/home/men
http://localhost:4200/home?product=watch
读取
this.route.paramMap.subscribe(params => {
console.log('路径参数: ', params);
this.selectedTabLink = params.get('tabLink');
});
this.route.queryParamMap.subscribe(params => {
console.log('查询参数', params);
});
http://localhost:4200/home/men;name=test?product=watch
测试一下,手动写成上面的URL打印一下
ngOnInit() {
this.route.paramMap.subscribe(params => {
console.log('路径参数: ', params);
this.selectedTabLink = params.get('tabLink');
});
this.route.queryParamMap.subscribe(params => {
console.log('查询参数', params);
});
}
输出信息如下

本文详细总结了Angular中的路径参数和查询参数,包括配置、跳转方法及URL表现形式。通过实例展示了如何读取和使用这两种参数,帮助开发者更好地理解和应用。
7192

被折叠的 条评论
为什么被折叠?



