【问题标题】:Issue with require.js when adding web3.js to project将 web3.js 添加到项目时出现 require.js 的问题
【发布时间】:2018-07-29 02:11:05
【问题描述】:

在我的 index.html 添加 web3.js cdn 时,出现错误:

未捕获的类型错误:require.config 不是函数

这仅在我包含 web3.js 脚本时发生。下面是我放入 index.html 的内容(我一直使用 getting-started 指南作为参考)。

<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<script>
      if (typeof web3 !== 'undefined') {
          web3 = new Web3(web3.currentProvider);
      } else {
          // set the provider you want from Web3.providers
          web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
          console.log(web3);
      }
</script>

在调试器中,我看到 web3 在错误之前正确加载。真的可以就如何继续使用建议。

【问题讨论】:

    标签: dependencies requirejs web3js


    【解决方案1】:

    Requirejs 不会知道加载 web3 的 cdn 路径,你可以为 requirejs 添加配置并使用 require 或 define 加载,

    例如

         requirejs.config({
        appDir: ".",
        baseUrl: "js",
        paths: { 
            /* Load from google cdn. On fail, load local file. */
            'web3': ['//cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"', 'web3.min']
    
        }
    
    });
    
    require(['web3'], function(web3) {
        console.log("Loaded :)");    
        return {};
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2010-10-17
      • 1970-01-01
      相关资源
      最近更新 更多