【问题标题】:webview: require is not definedwebview:要求未定义
【发布时间】:2018-05-15 16:52:21
【问题描述】:

在我的电子应用程序中,我正在设置一个 web 视图。这是 index.html 中的 sn-p:

  <webview
    class="hide"
    preload="./preload.js"
    nodeintegration
  ></webview>

  <script>
    require('./renderer.js');
  </script>

在 main.js 中,我为 BrowserWindow 设置了 nodeIntegration:false,但应用程序无法启动,因为我在 webview 的控制台中收到此错误:

Uncaught ReferenceError: require is not defined

如果我从 main.js 中删除 nodeIntegration:false,它可以工作,但我不想公开节点 API。

(P.S.:webview指向的远程web应用正在使用jQuery。可能有冲突?)

【问题讨论】:

    标签: node.js webview electron


    【解决方案1】:

    如果您删除 nodeIntegration : false 命令,如 require 将不会被设置。

    看来你有两种可能:

    • nodeIntegration: false处理它

    • 像在浏览器中一样调用你的 js 文件:&lt;script src="pathToFile"&gt;&lt;/script&gt;

    https://electronjs.org/docs/faq#i-can-not-use-jqueryrequirejsmeteorangularjs-in-electron 在这里他们解释了nodeIntegration 的真正作用

    【讨论】:

    • 对于您的选项#1:我不知道如何处理它,因为它会在 Web 应用程序中暴露 Node API。对于选项#2:它不起作用,因为它会在 renderer.js 中生成消息 "require is not defined"
    • 那么你应该使用browserify:browserify.org它使你可以在js前端文件中使用require
    猜你喜欢
    • 2019-04-15
    • 2016-08-10
    • 2018-06-15
    • 2019-08-20
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多