2017-06-29 128 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