![](/img/trans.png)
[英]Type error: cannot find module '...' or its corresponding type declarations
[英]Cannot find module 'crypto-js' or its corresponding type declarations
我有一個項目,這個項目正在顯示“二維碼”,然后出於某種原因我必須使用某個編碼器 function,為此我使用了這個庫:
Crypto
我正在嘗試使用 Crypto,但出現此錯誤:
Cannot find module 'crypto-js' or its corresponding type declarations.
文件.tsx:
import CryptoJS from 'crypto-js';
const ENC_KEY =
'50b3cc356d8f34017b3cce1a021389458b898ae85a816201695d11cb87fa1769';
const IV = '07ed0f192b6d8f36c24bd802e0a52cd4';
/**
*
* @param encryptedQR it should be a hex based string
* @returns a utf8 based decrypted string
*/
export function decrypt(encryptedQR: string) {
const key = CryptoJS.enc.Hex.parse(ENC_KEY);
const iv = CryptoJS.enc.Hex.parse(IV);
const encryptedHex = CryptoJS.enc.Hex.parse(encryptedQR);
const encrypted = CryptoJS.enc.Base64.stringify(encryptedHex);
const decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.NoPadding,
});
return CryptoJS.enc.Utf8.stringify(decrypted).trim();
}
我通過以下步驟解決了我的問題:
1- npm install crypto-js --save
2- npm install @types/crypto-js
3-
from: import { CryptoJS } from ‘crypto-js’;
to: import * as CryptoJS from ‘crypto-js’;
或者
1- yarn add crypto-js --save
2- yarn add @types/crypto-js
3-
from: import { CryptoJS } from ‘crypto-js’;
to: import * as CryptoJS from ‘crypto-js’;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.