angular传参的三种方式
1.routerLink
单一参数:在<a routerLink=["/exampledetail",id]>中加routerLink进行跳转,其中/exampledetail是我设置的路由path,id是需要传递的参数。
多个参数:如果要传多个参数,则可以写成routerLink=["/exampledetail",{queryParams:object}] ,queryParams携带多个参数,这个参数的格式可以是自行组织的object,也可以分开多个参数写成routerLink=["/exampledetail",{queryParams:‘id’:‘1’,‘name’:‘yxman’}];。
2.router.navigate
单一参数:this.router.navigate([’/exampledetail’,id]); ,多用在调用方法里
多个参数:this.router.navigate([’/exampledetail’],{queryParams:{‘name’:‘yxman’}});
3.router.navigateByUrl
单一参数:this.router.navigateByUrl(’/exampledetail/id’);
多个参数:this.router.navigateByUrl(’/exampledetail’,{queryParams:{‘name’:‘yxman’}});
传参方传参之后,接收方2种接收方式如下:
1.snapshot
import { ActivateRoute } from '@angular/router';
public data: any;
constructor( public route: ActivateRoute ) { };
ngOnInit(){
this.data = this.route.snapshot.params['id'];
};
2.queryParams
import { ActivateRoute } from '@angular/router';
public data: any;
constructor( public activeRoute:ActivateRoute ) { };
ngOnInit(){
this.activeRoute.queryParams.subscribe(params => {
this.data = params['name'];
});
};