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

浪尽此生 提交于 2019-12-11 05:52:15

问题


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.

I've installed CryptoJS using 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.

I've used --global instead but that then gives an error saying no typings were available!

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".


回答1:


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

Explanation:

  • --global CryptoJS is global module
  • dt~ The typings are inside DT repository: https://github.com/DefinitelyTyped/DefinitelyTyped
  • --save Because you want it to be stored inside typings.json, don't you?



回答2:


Here is the simplest step-by-step install and using example (working in nativescript/typescript/angular project):

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


来源:https://stackoverflow.com/questions/41038425/how-to-use-cryptojs-with-angular-2-and-typescript-in-webpack-build-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!