文中使用的工具或者包的版本:Angular CLI 11.0.6
文中 Demo 链接:https://stackblitz.com/edit/angular-route-params-xuspvu
{ path: ‘route1/:id‘, component: Route1Component }
<a [routerLink]="[‘/route1‘, 26]">route1</a>
或者:
this.router.navigate([‘/route1‘, 26]);
http://localhost:4200/route1/26
const id = this.route.snapshot.params.id;
{ path: ‘route2‘, component: Route2Component }
<a [routerLink]="[‘/route2‘, { a: 1, b: 2 }]">route2</a>
或者:
this.router.navigate([‘/route2‘, { a: 1, b: 2 }]);
http://localhost:4200/route2;a=1;b=2
const { a, b } = this.route.snapshot.params;
{ path: ‘route3‘, component: Route3Component }
<a [routerLink]="[‘/route3‘]" [queryParams]="{ a: 1, b: 2 }">route3</a>
或者:
this.router.navigate([‘/route3‘], { queryParams: { a: 1, b: 2 }});
http://localhost:4200/route3?a=1&b=2
const { a, b } = this.route.snapshot.queryParams;
{ path: ‘route4‘, component: Route4Component, data: { a: 1, b: 2 }}
const { a, b } = this.route.snapshot.data;
{ path: ‘route5‘, component: Route5Component, resolve: { a: aResolver }}
@Injectable({ providedIn: ‘root‘ })
export class aResolver {
resolve() {
return new Promise(res => {
setTimeout(() => { res(1); }, 3000);
});
}
}
const { a } = this.route.snapshot.data;
原文:https://www.cnblogs.com/yshenhua/p/14674955.html