尝试访问 Angular 中的查询参数时预期的表达式 2

Expression expected when trying to access query params in Angular 2

我尝试使用以下代码访问查询参数。

let name : string = this.route.snapshot?.queryParams.name
        console.log(name);

我收到以下错误:

error TS1109: Expression expected

error TS1005: ':' expected

does not exist on type 'Params'.

如何解决这个问题。

我认为你的问题在于你对待queryParams的方式。 它 returns 可观察,因此您可以像这样更改代码:

let name: string = '';
this.route.queryParams.subscribe(res => {
                          name = res.ResponseNameProperty;
                          console.log(name);                           
                       });

res.ResponseNameProperty 是从恢复的对象中获取 name 属性 的方式,因此您可以首先使用 console.log(res) 检查它的外观,然后使用适当的参考.我希望它符合您的问题,在这里您可以找到有关可观察对象的更多信息: thoughtram , official page,