简体   繁体   English

如何在 WebPack 构建环境中将 CryptoJS 与 Angular 2 和 TypeScript 一起使用?

[英]How to use CryptoJS with Angular 2 and TypeScript in WebPack build environment?

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.关于在 Angular 2 中使用 CryptoJS 库有很多问题,但大多数都假设使用 SystemJS,而且所有问题似乎都与当前版本的 TypeScript 过时了。 Can anybody give clear, simple instructions on how to use the CryptoJS library with Angular 2 and TypeScript.任何人都可以就如何在 Angular 2 和 TypeScript 中使用 CryptoJS 库给出清晰、简单的说明。

I've installed CryptoJS using npm install crypto-js .我已经使用npm install crypto-js安装了 CryptoJS。

I've tried the recommended typings i nstall crypto.js --ambient --save but this simply gives a warning about --ambient being deprecated and to use --global instead.我已经尝试了在nstall crypto.js --ambient --save推荐的nstall 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.对 WebPack、NodeJS 和 TypeScript 新手的任何建议将不胜感激。 I have the CryptoJS library installed under node_modules folder but any "sensible" attempts to "import" CryptoJS fail with "Module not found".我在 node_modules 文件夹下安装了 CryptoJS 库,但任何“明智的”尝试“导入”CryptoJS 都会失败,并显示“找不到模块”。 在此处输入图片说明

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");

The crypto-js package in npm has no built-in types and no longer maintained. npm 中的 crypto-js 包没有内置类型,不再维护。

You can try this, Witch is maintained by me, the same as crypto-js with TypeScript support and ES6 module: https://www.npmjs.com/package/crypto-es .你可以试试这个,Witch 是我维护的,和 crypto-js 一样,支持 TypeScript 和 ES6 模块: https ://www.npmjs.com/package/crypto-es。

typings install dt~crypto-js --global --save

Explanation:解释:

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

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