以太坊存证+ipfs
运行环境是ganache-cli自动生成十个账户。
import Web3 from 'web3'
let getWeb3 = new Promise(function(resolve, reject) {
// Wait for loading completion to avoid race conditions with web3 injection timing.
window.addEventListener('load', function() {
var results
var web3 = window.web3
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider.
web3 = new Web3(web3.currentProvider)
results = {
web3: web3
}
console.log('Injected web3 detected.');
resolve(results)
} else {
// Fallback to localhost if no web3 injection. We've configured this to
// use the development console's port by default.
var provider = new Web3.providers.HttpProvider('http://127.0.0.1:8545')
web3 = new Web3(provider)
results = {
web3: web3
}
console.log('No web3 instance injected, using Local web3.');
resolve(results)
}
})
})
export default getWeb3
之前一直使用的是工具类getweb3来获取web3的,然后控制台打印的是Injected web3 detected。
但是web3.eth.getAccounts()一直获取不到生成的十个账号
如果你的浏览器安装了Mist/metamask钱包插件,那么需要把工具类getweb3中的if (typeof web3 !== ‘undefined’){} 改成if (typeof web3 === ‘undefined’){}
把!改成=,就好了 控制台打印的是No web3 instance injected, using Local web3.
如果没有安装任何钱包或者插件 就不用改。
就好了
好了
了
真是吐了