Angular 9 使用查询字符串参数获取数据

Angular 9 get data with querystring params

我正在尝试从搜索中获取数据 API。搜索值作为查询字符串传递。

编译时无错误,运行时无错误。

const params = new HttpParams().set('search', 'pizza');
this.http
    .get('/hello', { responseType: 'json', params })
    .subscribe(console.log, console.log);

生成 /hello GET 查询但没有查询字符串参数。

怎么了?

Angular 准确的版本是 9.1.11

编辑

问题不是来自 Angular,而是关于我如何尝试嵌入该应用程序。它在使用默认开发服务器而不尝试嵌入它时有效。

我想您需要指定将 params 分配给哪个选项。

试试这个:

const apiParams = new HttpParams().set('search', 'pizza');
this.http
    .get('/hello', { responseType: 'json', params: apiParams })
    .subscribe(console.log, console.log);

Angular 或我使用的语法没有问题。问题来自我尝试做的整合。我尝试将 Angular 应用程序嵌入网页并从那里开发。

这似乎不是个好主意。

一切都适用于默认的开发服务器。