如何使用ethereumjs-tx |没有错误:Tx不是构造函数?

问题描述

我试图包括etheruemjs-tx浏览器内部版本,以便在我的浏览器上与之一起使用来签署交易。它不断抛出错误“ Tx不是构造函数

const Tx = "https://github.com/ethereumjs/browser-builds/blob/master/dist/ethereumjs-tx/ethereumjs-tx-1.3.3.js";

在我的终端上,一切正常,因为我刚刚使用

const Tx = require('ethereumjs-tx').Transaction

但是由于我无法在JavaScript代码上使用require(),因此我一直在尝试各种方法,但无法使其正常工作。在我的浏览器控制台上,单击按钮后,转到此行。

const tx = new Tx(txObject,{chain: 'ropsten',hardfork: 'petersburg'})

错误是:(索引):368未捕获的TypeError:Tx不是构造函数

我们将非常感谢您提供有关如何在浏览器中使用ethereumjs-tx的帮助。

解决方法

在新版本中,您应该更改:

const Tx = require('ethereumjs-tx')

const Tx = require('ethereumjs-tx').Transaction