2017-06-29 53 views
0

首先,我不確定這是否是正確的方式,以實現我想要的。 我想添加一個如下的導出綁定到router.navigate( ) - 方法:添加參數到角路由器。導航

<call [caller]="caller"></call> 

的問題是,我從來沒有使用的指令,而是通過路由器ADRESS它:

acceptCall() { 
    this.router.navigate(["call"]); 
} 

我怎樣才能達到同樣的出口從第一個例子結合acceptCall() - 方法? 我已經添加了一個輸入()變量,像這樣queryParams試了一下:

@Input() caller: Caller; 
acceptCall(caller) { 
    this.router.navigate(["call"], {queryParams: caller}); 
} 

但是,這是行不通的。

+0

差不多!嘗試'this.router.navigate([「call」,caller]);' – trichetriche

+0

不適合我,我必須修改我的RouterModule.forRoot嗎?我的目前看起來像︰{path:'call',component:CallComponent} – reveN

+0

嗯,是'{path:'call /:caller'}',你可以用this.route.params.subscribe((params:在你的子組件(路由是一個ActivatedRoute) – trichetriche

回答

2

按照我的意見,你必須:

1 - 重新定義你的路線

{path: 'call/:caller', component: MyComponent } 

2 - 在父組件

this.router.navigate(["call", caller]); 

3更改您的導航- 在子組件,獲取參數

import { ActivatedRoute } from '@angular/router'; 
// code until ... 
myParam: string; 
constructor(private route: ActivatedRoute) {} 
// code until ... 
ngOnInit() { 
    this.route.params.subscribe((params: Params) => this.myParam = params['caller']); 
} 
+0

mhh我完全按照你所說的完成了所有事情,但我的路由器現在回到我的「家」組件,而不是呼叫組件,我打電話給方法 – reveN

+1

我使用'this.router.navigate(['../ user',id],{relativeTo:this.route}');'確定它會重定向到正確的URL。你可以試試嗎? – trichetriche

+0

我創建了一個模擬調用者,並得到以下錯誤: 錯誤:無法匹配任何路由。 URL段:'call; Id = 1; Name = Frodo%20Baggins; Tel = 12345%2F678910; Company = The%20Fellowship%20of%20the%20Ring' – reveN