Angular6學習筆記17:在路由中傳引數和接收引數
阿新 • • 發佈:2018-12-07
傳引數
1.在需要傳引數的元件的類檔案(ts檔案)中,構造方法中,宣告物件:
constructor(private route: Router) {
}
2.在路由中設定引數(有三種方式)
- routerLink屬性
a.對於單一引數
<a [routerLink]="["/exampleRouterPath",id]"></a>
解釋說明:exampleRouterPath是需要跳轉的路由,id是需要傳遞的引數
b.對於多個引數
<a [routerLink]="["/exampleRouterPath",{queryParams:{id:1,name:'tom'}]" ></a>
解釋說明:exampleRouterPath是需要跳轉的路由,queryParams是傳遞的引數,型別是Object
- router.navigate
this.route.navigate(['exampleRouterPath'], {queryParams: {id: 0, name: 'tom'}});
解釋說明:exampleRouterPath是需要跳轉的路由,queryParams是傳遞的引數,型別是Object
- router.navigateByUrl
this.route.navigateByUrl('exampleRouterPath', {queryParams: {id: 0 , name:'tom'}});
解釋說明:exampleRouterPath是需要跳轉的路由,queryParams是傳遞的引數,型別是Object
接收引數
1.在跳轉的路由的類檔案中(.ts 檔案)的構造方法中宣告一個物件
constructor(private activeRoute: ActivatedRoute){}
2.接收引數
this.activeRoute.queryParams.subscribe(params => {
console.log(params); // params 路由傳的引數
});