简体   繁体   English

如何在 Angular 中使用“jquery-param”npm package?

[英]How to use “jquery-param” npm package in Angular?

The jquery-param npm package has the functionality I need in my Angular9 project. jquery-param npm package 具有我在 Angular9 项目中需要的功能。 I noticed it doesn't have typings for typescript so I also added @types/jquery-param as dependency.我注意到它没有 typescript 的类型,所以我还添加了@types/jquery-param作为依赖项。 I've tried importing in different ways to no avail.我尝试以不同的方式导入无济于事。 I've also read various answers about DefinitelyTyped and typings but can't manage to find the solution.我还阅读了有关definitelyTyped 和typings的各种答案,但无法找到解决方案。 Made a StackBlitz with intended usage if someone is willing to take a look.如果有人愿意看一下,请按预期用途制作StackBlitz

Update:更新:
TL;DR: I managed to import and use it. TL;DR:我设法导入并使用它。 Now the project compiles and works great , but the VSCode's checker marks an error in the import statement .现在项目编译并且运行良好,但是 VSCode 的检查器在 import 语句中标记了一个错误 After closing and reopening VSCode it no longer shows error in the import.关闭并重新打开 VSCode 后,它不再显示导入错误。 It seems it only load changes to tsconfig.json on app start.似乎它只在应用程序启动时加载对tsconfig.json的更改。

In StackBlitz works great with just the typings and importing with import param from 'jquery-param';StackBlitz中,仅输入类型和使用import param from 'jquery-param'; . .

Went ahead and installed both packages in my real project.继续在我的真实项目中安装这两个软件包。 Then had trouble importing as the compiler complained:然后由于编译器抱怨导入时遇到问题:

ERROR in src/app/shared/services/http.service.ts:4:24 - error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'allowSyntheticDefaultImports' flag and referencing its default export. src/app/shared/services/http.service.ts:4:24 中的错误 - 错误 TS2497:此模块只能通过打开“allowSyntheticDefaultImports”标志并引用其默认导出来引用 ECMAScript 导入/导出。

This is because of how the typings are exported, as @wtho mentioned and as stated by:这是因为类型是如何导出的,正如@wtho 提到的那样,如下所述:

index.d.ts(8, 1): This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag. index.d.ts(8, 1):此模块使用 'export =' 声明,并且只能在使用 'allowSyntheticDefaultImports' 标志时与默认导入一起使用。

Tried importing with import * as params from 'jquery-params' and other ways using curly braces, didn't work.尝试使用import * as params from 'jquery-params'和使用花括号的其他方式导入,但没有成功。

So I added所以我加了

    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,

to tsconfig.json as some answers mentioned.tsconfig.json作为提到的一些答案。

Now the project compiles and works great.现在项目编译并且运行良好。

Note about jquery-param :关于jquery-param 的注意事项
This package exports a sigle function equivalent to jQuery.param() .这个 package 导出一个 function 等效于jQuery.param() This allows to encode a nested object to pass it as a query string in a GET request.这允许对嵌套的 object 进行编码,以在 GET 请求中将其作为查询字符串传递。 I will be using it in requests to an API like this:我将在对 API 的请求中使用它,如下所示:

const params = {
  pageIndex: 1,
  pageSize: 10,
  filter: [
    { key: 'active', value: true }
  ],
  sort: [
    { key: 'username', value: 'ascend' }
  ]
}

url += '?' + param(params);

this.httpClient.get(url).subscribe(
  ...
);

I went for this package because couldn't do it using Angular API, and most answers said spin your own function to encode nested objects. I went for this package because couldn't do it using Angular API, and most answers said spin your own function to encode nested objects.

Before ES modules were introduced, packages often exported a single function as default export.在引入 ES 模块之前,包通常会导出单个 function 作为默认导出。 You can import these using the ES modules syntax import param from 'jquery-param' and just setting "esModuleInterop": true in your base tsconfig.json .您可以使用 ES 模块语法import param from 'jquery-param'导入这些,只需在基础tsconfig.json中设置"esModuleInterop": true即可。

If VSCode still complains, make sure you do not have some outdated TypeScript extensions active.如果 VSCode 仍然抱怨,请确保您没有激活一些过时的 TypeScript 扩展。

Here is a working stackblitz demo .这是一个有效的stackblitz 演示

It also worked for me locally with Angular v10.0.2 and TypeScript v3.9.5 .它也适用于本地 Angular v10.0.2和 TypeScript v3.9.5


Note that url query parameters are not designed to transport complex object.请注意,url 查询参数并非设计用于传输复杂的 object。 If possible, switching to a HTTP POST request might be the better solution.如果可能,切换到 HTTP POST 请求可能是更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM