【问题标题】:Electron iframe: require is not defined电子 iframe:未定义要求
【发布时间】:2017-12-28 13:47:30
【问题描述】:

我想在我的电子应用程序中嵌入一个 HTML 文件。我选择使用 iframe,但是 - 当我这样做时 - 似乎我不能再使用 node.js。任何使用 require("electron") 的尝试都会表明 require 没有被定义。有什么想法吗?

提前致谢!

  • 斯蒂金

【问题讨论】:

    标签: node.js iframe electron require


    【解决方案1】:

    如果您不介意迟到的答案,您可能应该使用<webview>,因为它可以让您更精细地控制所需内容。 但是,如果您确实需要使用<iframe>,您有两种选择:

    1. 使用 parent.require(父级访问 iframe 父级,它应该是您的窗口)
    2. 在加载事件上,将父窗口的需求复制到 iframe 之一:
    iframe.onload = function () {
      const iframeWin = iframe.contentWindow
      iframeWin.require = window.require
    })
    

    唯一的“缺点”是您包含的包将访问主窗口 DOM 而不是 iframe 的,这意味着 iframe 窗口全局变量将不存在,更重要的是,文档将访问窗口 DOM ( querySelector 和 getElemementBy 方法不会真正起作用)。这对您来说是否是个问题取决于您如何组织代码,祝您好运

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 2019-10-11
      • 2020-10-22
      • 2019-08-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 2019-08-09
      • 2020-07-19
      相关资源
      最近更新 更多