Import crypto-js in an angular 2 project (created with angular-cli)

雨燕双飞 提交于 2019-12-05 03:03:56
Weslley De Souza

This may help you:
https://github.com/Uisli21/SecureAngularLogin


$ npm install crypto-js --save
$ npm install @types/crypto-js --save-dev

then:

import * as CryptoJS from 'crypto-js';

or

import CryptoJS = require('crypto-js');

You can try following as a solution:

1. npm install --save @types/crypto-js


2. import { AES } from "crypto-js";


3. AES.encrypt('my message', 'secret key');

Angular-cli still have some issues to integrate third-party plugins. So, Dont forget to add it in the index.html . Add like this way

<script src="vendor/crypto-js/crypto-js.js"></script> 

I think it will solve your problem :)

Update

const map: any = {
    'crypto-js': 'vendor/crypto-js'
};



/** User packages configuration. */
const packages: any = {
    'crypto-js': {
        format: 'cjs',
        defaultExtension: 'js',
        main: 'crypto-js.js'
    }
};

Ok I got it. I just download the DefinitelyTyped file in typings/crypto-js/ and then I add the line /// <reference path="../../typings/crypto-js/crypto-js.d.ts" /> before importing CryptoJS.

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