我刚刚开始探索区块链技术,并于前几天制作了我的第一个智能合约。接下来,我尝试为智能合约创建一个前端,但在使用 web3.js 将我的 Angular 应用程序连接到 Metamask 时遇到困难。
具体来说,我遇到一个问题,当我尝试为我的 Angular 应用程序提供服务时,它给出了以下错误:
错误:./node_modules/eth-lib/lib/bytes.js 找不到模块:错误:无法解析“C:\Users\profile\Desktop\Coding\EthSmartContractProject\Frontend\node_modules\eth-lib”中的“加密” \lib'
错误:./node_modules/eth-lib/lib/bytes.js 找不到模块:错误:无法解析“C:\Users\profile\Desktop\Coding\EthSmartContractProject\Frontend\node_modules\eth-lib”中的“流” \lib'
这是我的 Blockchain.service.ts,我在其中尝试处理角度应用程序中的所有区块链相关任务:
import { Injectable } from '@angular/core';
import Web3 from "web3";
declare let window:any;
@Injectable({
providedIn: 'root'
})
export class ContractService {
web3: any;
accounts: Array<String>;
async loadWeb3() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum);
await window.ethereum.enable;
} else if (window.web3) {
window.web3 = new Web3(window.web3.currentProvider);
} else {
window.alert('Non-Ethereum browser detected. You Should consider using MetaMask!');
}
}
}
Run Code Online (Sandbox Code Playgroud)
重现步骤: