typescript 如何在 Angular2 中使用“crypto”模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43353718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use 'crypto' module in Angular2?
提问by johnerfx
I install module:
我安装模块:
npm install --save crypto
I import it to my component:
我将它导入到我的组件中:
import { createHmac } from "crypto";
But I get error:
但我得到错误:
ERROR in -------------- (4,28): Canno t find module 'crypto'.
-------------- (4,28) 中的错误:找不到模块“加密”。
What am I doing wrong?
我究竟做错了什么?
回答by Guillaume
To use crypto
NodeJS library with Typescript (Angular >= 2 for example) follow these steps:
要将crypto
NodeJS 库与 Typescript(例如 Angular >= 2)一起使用,请执行以下步骤:
npm install @types/node --save-dev
to install NodeJS definitionsIn
tsconfig.ts
file add the following:"files": [ "./node_modules/@types/node/index.d.ts" ]
Import the library where you want to use it with
import * as crypto from 'crypto';
npm install @types/node --save-dev
安装 NodeJS 定义在
tsconfig.ts
文件中添加以下内容:"files": [ "./node_modules/@types/node/index.d.ts" ]
导入要与它一起使用的库
import * as crypto from 'crypto';
回答by eko
You need to install the definition files for a 3rd party library like crypto
. So that typescript can find the "meaning" for it.
您需要安装 3rd 方库的定义文件,例如crypto
. 这样打字稿就可以找到它的“意义”。
I think the definition file is:
我认为定义文件是:
npm install --save-dev @types/crypto-js
Then you can import the module like:
然后你可以像这样导入模块:
import * as crypto from "crypto";
If you can't find the definition file for that lib, you can write it on your own or as a workaround you can declare the module as any
but typescript won't be able to auto-complete the methods.
如果您找不到该库的定义文件,您可以自己编写它,或者作为一种解决方法,您可以将模块声明为,any
但打字稿将无法自动完成这些方法。
declare var crypto: any;
and use its methods like:
并使用其方法,例如:
crypto.createHmac..
回答by Javi
I am developing with the latest versions of Angular and 'crypto-js' seems to work fine.
我正在使用最新版本的 Angular 进行开发,“crypto-js”似乎运行良好。
Install the package and the definitions:
安装包和定义:
npm install crypto-js
npm install --save @types/crypto-js
Use it:
用它:
import { SHA256, enc } from "crypto-js";
...
login() {
...
const hashedPass = SHA256(this.loginForm.value.password).toString(enc.Hex);
...
}
回答by Acyuta
The current tsconfig.jsonconfiguration(I'm using "typescript": "~3.5.3") includes a typescompiler option that should be used in this case: In tsconfig.ts file add the following:
在当前tsconfig.json配置(我使用“打字稿”:“〜3.5.3”)包括类型应该在这种情况下使用编译器选项:在tsconfig.ts文件中添加以下内容:
{
"compilerOptions": {
"types" : [ "node" ]
}
}
Import the library where you want to use it with import crypto from 'crypto'
导入要与它一起使用的库 import crypto from 'crypto'
Don't use import * as crypto from 'crypto': it will import deprecated symbols/functions. (you should probably see the compiler complain about it)
不要使用 import * 作为来自 'crypto' 的加密:它会导入不推荐使用的符号/函数。(你可能应该看到编译器抱怨它)