Angular2 routing 尝试访问Angular 2中的查询参数时需要表达式

Angular2 routing 尝试访问Angular 2中的查询参数时需要表达式,angular2-routing,Angular2 Routing,我尝试使用下面的代码访问查询参数 let name : string = this.route.snapshot?.queryParams.name console.log(name); 我得到了以下错误: error TS1109: Expression expected error TS1005: ':' expected does not exist on type 'Params'. 如何解决这个问题。我认为您的问题在于处理查询参数的方式。 它返回observab

我尝试使用下面的代码访问查询参数

let name : string = this.route.snapshot?.queryParams.name
        console.log(name);
我得到了以下错误:

error TS1109: Expression expected

error TS1005: ':' expected

does not exist on type 'Params'.

如何解决这个问题。

我认为您的问题在于处理
查询参数的方式。
它返回observable,因此您可以这样更改代码:

let name: string = '';
this.route.queryParams.subscribe(res => {
                          name = res.ResponseNameProperty;
                          console.log(name);                           
                       });
res.ResponseNameProperty
是从恢复的对象中获取
name
属性的方法,因此您可以首先使用控制台.log(res)
检查它的外观,然后使用适当的引用。我希望它符合您的问题,在这里您可以找到有关可观测的其他信息: ,