文章目录
一、问题:vue3.0中引用 jsencrypt加密库报错
二、原因:jsencrypt库不支持es6 ES module模块导出,vue3不支持commonjs导入导出
const JSEncrypt= require('@/utils/jsencrypt.min.js')
const publicKey = '....'
const priviteKey= '....'
// 加密
export function encrypt(txt) {
const encryptor = new JSEncrypt();
encryptor.setPublicKey(publicKey); // 设置公钥
return encryptor.encrypt(txt); // 对需要加密的数据进行加密
}
//解密
export function decrypt(txt) {
const encryptor = new JSEncrypt();
encryptor.setPrivateKey(priviteKey);
return encryptor.decrypt(txt);
}
三、解决:改配置文件
在脚本文件中修改为:
! function(t, e) {
t.JSEncrypt = e()
}(modificationWindow, (function() {
//....
})
export default modificationWindow
文件资源:(
已经改好的文件
)
链接: https://pan.baidu.com/s/1JuwrWltoHNHMtmPiHtvymA?pwd=u651
提取码: u651
四、具体使用
import JSEncrypt from '@/utils/jsencrypt.min.js';
export function encrypt(txt) {
const encryptor = new JSEncrypt.JSEncrypt ();
encryptor.setPublicKey(publicKey); // 设置公钥
return encryptor.encrypt(txt); // 对需要加密的数据进行加密
}
//解密
export function decrypt(txt) {
const encryptor = new JSEncrypt.JSEncrypt ();
encryptor.setPrivateKey(priviteKey);
return encryptor.decrypt(txt);
}
成功解决