在浏览其中经常碰到未定义的对象:*** is not defined
这通常是因为该对象通常是在 Node.js 环境中使用的,而在浏览器环境中并不会自动提供该对象。
方法一
那我们可以导入相应的对象,定义全局变量:
import * as bip39 from ‘bip39’;
import { Buffer } from ‘buffer’; // 在浏览器中引入 buffer
// 设置全局变量,以使 bIP39 库能够使用 Buffer
window.Buffer = Buffer;
// 生成助记词
const mnemonic = bip39.generateMnemonic();
console.log(‘Generated mnemonic:’, mnemonic);
方法二
如果是用脚手架搭建的项目:
1.npm install node-polyfill-webpack-plugin
2.编辑vue.config.js文件:
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
plugins: [
new NodePolyfillPlugin(),
],
}
})
3.重新运行项目