Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向Angular router.navigate添加参数_Angular_Typescript_Binding_Router - Fatal编程技术网

向Angular router.navigate添加参数

向Angular router.navigate添加参数,angular,typescript,binding,router,Angular,Typescript,Binding,Router,首先,我不确定这是否是实现我想要的目标的正确途径。。 我正在尝试向路由器添加导出绑定,如下所示。navigate()-方法: 如何从acceptCall()方法中的第一个示例实现相同的导出绑定? 我已经添加了一个Input()变量,并尝试使用queryParams,如下所示: @Input() caller: Caller; acceptCall(caller) { this.router.navigate(["call"], {queryParams: caller}); } 但这不

首先,我不确定这是否是实现我想要的目标的正确途径。。 我正在尝试向路由器添加导出绑定,如下所示。navigate()-方法:

如何从acceptCall()方法中的第一个示例实现相同的导出绑定? 我已经添加了一个Input()变量,并尝试使用queryParams,如下所示:

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

但这不起作用。

根据我的评论,您必须:

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']);
}

几乎!尝试
this.router.navigate([“call”,caller])不适用于我,我必须修改RouterModule.forRoot吗?我的当前看起来像:{path:'call',component:CallComponent}是的,
{path:'call/:caller'}
你可以通过
这个.route.params.subscribe((params:params)=>this.myParam=params['caller'])获得它在您的子组件(路由是一个激活的路由)中,我已经更新了我的模块,但没有完全了解其他内容:D您在哪里声明参数,myParam?你能提供一个完整的类代码示例作为答案吗?@reveN:你的代码组织方式不是很明显。如果你清楚地说出你提供的所有代码位的位置,你会更容易帮到你。我做的一切都和你说的一模一样,但是我的路由器现在回到了我的“家”组件,而不是调用组件,当我调用这个方法时,我使用了像
this.router.navigate(['../user',id],{relativeTo:this.route})这样的调用
以确保它将重定向到正确的url。你能试试吗?我创建了一个模拟调用者并得到以下错误:错误:无法匹配任何路由。URL段:'呼叫;Id=1;Name=Frodo%20Baggins;电话=12345%2F678910;公司=20%团契20%的20%20戒指'Oh!然后使用服务或父子关系。路由参数并不是为此而设计的。为了完整起见,您还需要从@angular/router导入参数
this.router.navigate(["call", caller]);
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']);
}