Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 5中的ActivateRoute未传递URL参数。思想?_Angular_Url_Get_Angular5_Angular Activatedroute - Fatal编程技术网

Angular 5中的ActivateRoute未传递URL参数。思想?

Angular 5中的ActivateRoute未传递URL参数。思想?,angular,url,get,angular5,angular-activatedroute,Angular,Url,Get,Angular5,Angular Activatedroute,我已经在app.component.ts页面上实现了ActivateRoute。在这个页面上,我有一个红色的大退出按钮,它会按预期出现在我应用程序的所有页面上 我跳转到我创建的url(带有查询字符串),如下所示: http://localhost:4200/status?returnString=xyz 我的构造函数中有一个私有路由:ActivatedRoute。在我的ngOnInit()函数中,我有console.log(this.route.queryParams)。它返回一个对象,其中\u

我已经在app.component.ts页面上实现了ActivateRoute。在这个页面上,我有一个红色的大退出按钮,它会按预期出现在我应用程序的所有页面上

我跳转到我创建的url(带有查询字符串),如下所示:

http://localhost:4200/status?returnString=xyz

我的构造函数中有一个
私有路由:ActivatedRoute
。在我的
ngOnInit()
函数中,我有
console.log(this.route.queryParams)。它返回一个对象,其中
\u值
是一个属性为
returnString的对象:“xyz”

问题是,当我
console.log(this.route.queryParams.returnString)
console.log(this.route.queryParams.u值)
时,向我提供的对象没有属性<代码>{}


我的问题是,发生了什么,或者说,我做错了什么?从URL中获取一个可以在Angular 5中使用的参数需要什么?

您遇到的问题是由于
queryParams
是一个
可观察的

ngOnInit()
函数中,您需要订阅可观察到的:

ngOnInit() {
    this.route. queryParams.subscribe(
      params => {//Do something with the params.returnString},
      err => console.log(err)
);
}

您遇到的问题是由于
queryParams
是一个
可观察的

ngOnInit()
函数中,您需要订阅可观察到的:

ngOnInit() {
    this.route. queryParams.subscribe(
      params => {//Do something with the params.returnString},
      err => console.log(err)
);
}
是可观察的,因此您应该倾听变化,而不是检查值

启动app.component时,该值尚未设置,但当您检查它时,该值已设置。这可能就是你正在经历的

设置侦听器并对更改作出反应

route.queryParams.subscribe((params: Params) => this.doSomething(params.returnString))
是可观察的,因此您应该倾听更改,而不是检查值

启动app.component时,该值尚未设置,但当您检查它时,该值已设置。这可能就是你正在经历的

设置侦听器并对更改作出反应

route.queryParams.subscribe((params: Params) => this.doSomething(params.returnString))