如何在 WebPack 构建环境中将 CryptoJS 与 Angular 2 和 TypeScript 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41038425/
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 CryptoJS with Angular 2 and TypeScript in WebPack build environment?
提问by Ian Smith
There are a whole bunch of questions around using the CryptoJS library with Angular 2 but most assume use of SystemJS and all seem out of date with the current version of TypeScript. Can anybody give clear, simple instructions on how to use the CryptoJS library with Angular 2 and TypeScript.
关于在 Angular 2 中使用 CryptoJS 库有很多问题,但大多数都假设使用 SystemJS,而且所有问题似乎都与当前版本的 TypeScript 过时了。任何人都可以就如何在 Angular 2 和 TypeScript 中使用 CryptoJS 库给出清晰、简单的说明。
I've installed CryptoJS using npm install crypto-js
.
我已经使用npm install crypto-js
.
I've tried the recommended typings install crypto.js --ambient --save
but this simply gives a warning about --ambient being deprecated and to use --global instead.
我已经尝试了推荐的类型 install crypto.js --ambient --save
但这只是给出了关于 --ambient 被弃用并使用 --global 的警告。
I've used --global instead but that then gives an error saying no typings were available!
我已经使用了 --global ,但随后会出现错误,提示没有可用的类型!
Any advice to someone new to WebPack, NodeJS AND TypeScript would be appreciated. I have the CryptoJS library installed under node_modules folder but any "sensible" attempts to "import" CryptoJS fail with "Module not found".
对 WebPack、NodeJS 和 TypeScript 新手的任何建议将不胜感激。我在 node_modules 文件夹下安装了 CryptoJS 库,但任何“明智的”尝试“导入”CryptoJS 都会失败,并显示“找不到模块”。
采纳答案by rzelek
typings install dt~crypto-js --global --save
Explanation:
解释:
--global
CryptoJS is global moduledt~
The typings are inside DT repository: https://github.com/DefinitelyTyped/DefinitelyTyped--save
Because you want it to be stored insidetypings.json
, don't you?
--global
CryptoJS 是全局模块dt~
类型在 DT 存储库中:https: //github.com/DefinitelyTyped/DefinitelyTyped--save
因为你想把它存储在里面typings.json
,不是吗?
回答by Bruno Leit?o
Here is the simplest step-by-step install and using example (working in nativescript/typescript/angular project):
这是最简单的分步安装和使用示例(在 nativescript/typescript/angular 项目中工作):
npm install crypto-js
then:
然后:
npm install --save @types/crypto-js
Import in some component:
导入一些组件:
import * as crypto from "crypto-js";
And use it:
并使用它:
crypto.DES.decrypt("Your secret", "YOUR_CRYPTO_KEY");
回答by Entronad
The crypto-js package in npm has no built-in types and no longer maintained.
npm 中的 crypto-js 包没有内置类型,不再维护。
You can try this, the same as crypto-js with TypeScript support and ES6 module: https://www.npmjs.com/package/crypto-es.
你可以试试这个,和带有 TypeScript 支持和 ES6 模块的 crypto-js 一样:https://www.npmjs.com/package/crypto-es 。