How to use 'crypto' module in Angular2?

前端 未结 4 679
无人及你
无人及你 2020-12-06 02:27

I install module:

npm install --save crypto

I import it to my component:

import { createHmac } from \"crypto\";
         


        
4条回答
  •  Happy的楠姐
    2020-12-06 02:38

    You need to install the definition files for a 3rd party library like crypto. So that typescript can find the "meaning" for it.

    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.

    declare var crypto: any;
    

    and use its methods like:

    crypto.createHmac..
    

提交回复
热议问题