美文网首页
angular 路由传参

angular 路由传参

作者: 云上笔记 | 来源:发表于2021-02-04 15:37 被阅读0次
import { ActivatedRoute, Router }from '@angular/router';

private route: ActivatedRoute;
private router: Router ;

路由必填参数

// 路由配置
{ path: 'detail/:id', component: DetailComponent }

// 传参方式
<a [routerLink]="['/detail', detail.id]"></a>
this.router.navigate(['/detail', this.detail.id]);

// 读取参数
this.route.snapshot.paramMap.get('id');

// 页面路径
`http://localhost:4200/detail/1

路由可选参数

// 路由配置
{ path: 'detail', component: DetailComponent }

// 传参方式
<a [routerLink]="['/detail', {age: 18, male: true}]"></a>
this.router.navigate(['/detail',  {age: 18, male: true}]);

// 读取参数
this.route.snapshot.paramMap.get('age');
this.route.snapshot.paramMap.get('male');

// 页面路径
`http://localhost:4200/detail;age=18;male=true

路由查询参数

// 路由配置
{ path: 'detail', component: DetailComponent }

// 传参方式
<a [routerLink]="['/detail']" [queryParams]=" {age: 18, male: true}"></a>
this.router.navigate(['/detail'], {queryParams: {age: 18, male: true}});

// 读取参数
this.route.snapshot.queryParamMap.get('age');
this.route.snapshot.queryParamMap.get('male');

// 页面路径
`http://localhost:4200/detail?age=18&male=true

相关文章

网友评论

      本文标题:angular 路由传参

      本文链接:https://www.haomeiwen.com/subject/xnisaxtx.html