如何将两个函数调用放在HTML文件中



我正在学习坚固性(对于智能合约),现在需要设计一个UI来与部署合同进行交互。

注意:如果问题与此论坛无关,请让我知道(而不是退票),我将删除它。


我的html和.js文件如下。问题在于,当我同时包含" distribute()"one_answers" update_exchange_rate()"功能中时,我的html文件将不起作用。但是如果我从.js文件中删除它们。

我不会有任何问题。

问题:我为什么遇到这个问题?如何解决上述问题?我可以在window.app中具有多个函数(定义)吗?


编辑:如果我将两个函数都放在.js文件中,我也会收到WebPack错误。但是,如果我删除其中一个功能,则错误将消失。


<!DOCTYPE html>
<html>
<head> 
<script src="./app.js"></script>
</head>
<body>
<h1>MetaCoin</h1>
<h2>Example Truffle Dapp</h2>
<br>
<br><label for="amountB">Exchange rate:</label><input type="text" 
id="amountA" placeholder="e.g., 95"></input>
<br><label for="receiverA">ReceiverA:</label><input type="text" 
id="receiverA" placeholder="e.g., 95"></input>
<br><label for="receiverB">ReceiverB:</label><input type="text" 
id="receiverB" placeholder="e.g., 95"></input>
<br><br><button id="send1" onclick="App.distribute()">Send 
MetaCoin</button>
<br><br>
<br><label for="amountB">Exchange rate:</label><input type="text" 
id="amountB" placeholder="e.g., 95"></input>
<br><br><button id="send2" 
onclick="App.update_exchange_Rate()">update_exchange_Rate</button>
<br><br>
<br>
</body>
</html>

我的JS文件是:

  import "../stylesheets/app.css";
  import { default as Web3} from 'web3';
   import { default as contract } from 'truffle-contract'
  import metacoin_artifacts from '../../build/contracts/MetaCo.json'
  var MetaCo = contract(metacoin_artifacts);
  var accounts;
  var account;
  window.App = {
  start: function() {
  MetaCo.setProvider(web3.currentProvider);
  web3.eth.getAccounts(function(err, accs) {
  if (err != null) {
    alert("There was an error fetching your accounts.");
    return;
  }
  if (accs.length == 0) {
    alert("Couldn't get any accounts! Make sure your Ethereum client is 
    configured correctly.");
    return;
  }
  accounts = accs;
  account = accounts[0];
 });
},
setStatus: function(message) {
var status = document.getElementById("status");
status.innerHTML = message;
},

distribute: function() { // XXX Here is where the problem occures!
var amountA = parseInt(document.getElementById("amountA").value);
var receiver1= document.getElementById("receiverA").value;
var receiver2 = document.getElementById("receiverB").value;
var meta;
MetaCo.deployed().then(function(instance2) {
  meta = instance2;
return meta.distribute(receiver1,receiver2, amountA,{from: account});
})
}
 update_exchange_Rate: function() { // XXX Here is where the problem occures!
  var amountB = parseInt(document.getElementById("amountB").value);
 var meta1;
 MetaCo.deployed().then(function(instance3) {
  meta1 = instance3;
return meta1.update_exchange_Rate(amountB,{from: account});
})
 }
};
window.addEventListener('load', function() {
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source. If you find 
that your accounts don't appear or you have 0 MetaCoin, ensure you've 
configured that source properly. If using MetaMask, see the following 
link. 
Feel free to delete this warning. :) 
http://truffleframework.com/tutorials/truffle-and-metamask")
// Use Mist/MetaMask's provider
 window.web3 = new Web3(web3.currentProvider);
 } else {
console.warn("No web3 detected. Falling back to http://localhost:8545. 
You should remove this fallback when you deploy live, as it's 
inherently 
insecure. Consider switching to Metamask for development. More info 
here: 
http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + 
in-dapp id mgmt / fail)
window.web3 = new Web3(new 
Web3.providers.HttpProvider("http://localhost:8545"));
  }
App.start();
});

让我们只按照此按钮,另一个按钮有完全相同的问题...

<button id="send2" onclick="App.update_exchange_Rate()">update_exchange_Rate</button>

ONCLICK属性期望接收功能。然后,每次点击都会调用该功能。但是该函数不会返回函数本身...

app.update_exchange_rate()将在浏览器看到该部分后立即执行,并且使用的返回值使用。但这不是我们想要的!我们希望执行该功能...因此我们需要提供该功能,而不是对属性的调用,例如:

<button id="send2" onclick="App.update_exchange_Rate">update_exchange_Rate</button>

<button id="send2" onclick="function(){App.update_exchange_Rate();}">update_exchange_Rate</button>

现在,如果您这样做,您肯定会遇到您没有想到的范围。您在功能中没有使用this,所以我会跳过该范围的部分,如果需要,您可以稍后阅读。

最新更新