[英]Connecting User Ethereum account with Metamask in ReactJS and Javascript
First of all, I am pretty new to programming, so please bear with me =)首先,我对编程还很陌生,所以请耐心等待 =)
I am building an app, which requires users to connect their Metamask accounts.我正在构建一个应用程序,它需要用户连接他们的 Metamask 帐户。 I would like to be able to store web3 related functions in a separate js file.我希望能够将 web3 相关功能存储在单独的 js 文件中。 I am currently working on a reusable Metamask Connect Button.我目前正在研究可重复使用的 Metamask Connect 按钮。 Here is what the code of the button looks like:按钮代码如下所示:
export default function MetamaskBtn() {
function connect() {
window.ethereum.request({ method: 'eth_requestAccounts' })
}
console.log(connect);
return (
<button
onClick={connect}
style={{
id: 'metamaskbtn',
gridArea: 'web3',
fontSize: '60%',
border: 'solid 1px #174666',
background: '#E36E1A',
borderRadius: '3px',
placeSelf: 'center'
}}>
Connect Wallet
</button>
)
}
which works fine, now I am trying to save that connect()
function in an external js file, which i called ConnectMetamask
:工作正常,现在我试图将该connect()
函数保存在一个外部 js 文件中,我称之为ConnectMetamask
:
export function ConnectMetamask() {
window.ethereum.request({ method: 'eth_requestedAccounts' })
}
Then I tried importing the file into my jsx file with no success :然后我尝试将文件导入我的 jsx 文件,但没有成功:
import { ConnectMetamask } from "../../../../_JS Functions/web3Function";
const Metamask = ConnectMetamask
export default function MetamaskBtn() {
console.log(Metamask);
return (
<button
onClick={Metamask}
style={{
id: 'metamaskbtn',
gridArea: 'web3',
fontSize: '60%',
border: 'solid 1px #174666',
background: '#E36E1A',
borderRadius: '3px',
placeSelf: 'center'
}}>
Connect Wallet
</button>
)
}
I console.log()
both functions and they print the same thing in fact.我console.log()
两个函数,它们实际上打印相同的东西。
connect()
: connect()
:
ƒ connect() {
window.ethereum.request({
method: 'eth_requestAccounts'
});
}
ConnectMetamask()
ƒ ConnectMetamask() {
window.ethereum.request({
method: 'eth_requestedAccounts'
});
}
The error I get from the latter is:我从后者得到的错误是:
index.js:1 MetaMask - RPC Error: The method 'eth_requestedAccounts' does not exist / is not available. {code: -32601, message: "The method 'eth_requestedAccounts' does not exist / is not available.", data: {…}}
and和
localhost/:1 Uncaught (in promise) {code: -32601, message: "The method 'eth_requestedAccounts' does not exist / is not available.", data: {…}}
Hope someone can help me out with this!希望有人能帮我解决这个问题!
好吧,事实上我刚刚意识到这是一个拼写错误 =) hehe-.. 应该是'eth_requestAccounts'
而不是'eth_requestedAccounts'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.