path参数
路由中定义
{ path: 'date/:id', component: ******}
路由跳转
<a routerLink="date/4">gg</a>
this.router.navigate(['date'],4);
this.router.navigateByUrl('list/4');
获取路径参数和监听路径参数改变
import { Router,ActivatedRoute } from '@angular/router';
constructor(private routerinfo: ActivatedRoute,private router: Router){
let params = this.routerinfo.snapshot.params;//参数对象
this.id = params['id'];//获取参数
//监听参数变化
this.routerinfo.params.subscribe((res) => {
this.id = res['id'];
});
}
Query参数
<a routerLink="/home" [queryParams]="{limit:5}">gg</a>
this.router.navigate(['/home'],{queryParams: {limit: 5}});
this.router.navigateByUrl('/home?limit=5');
constructor(private routerinfo: ActivatedRoute,private router: Router){
this.routerinfo.queryParams.subscribe((res) => {
});
}
Matrix参数
<a [routerLink]="['/home',{name: 'Max'}]">gg</a>
//点进去就成这样 http://localhost:4200/home;name=Max