简体   繁体   English

未在电子中定义Require()-节点JS

[英]Require() is not defined in electron - node js

I am using electron with node js. 我在node js中使用电子。 I have used require() already in two html files like this: 我已经在这样的两个html文件中使用了require():

<script>
        window.$ = window.jQuery = require("jquery");
</script>

Now when I try to do the same thing in another html file in the same project, I get "ReferenceError: require is not defined" when the page loads. 现在,当我尝试在同一项目中的另一个html文件中执行相同的操作时,在页面加载时出现“ ReferenceError:未定义require”。 This other page is being shown in an iframe, but I'm not sure that would matter. 另一页正在iframe中显示,但我不确定这是否重要。

And for reference I have set nodeIntegration to true in main.js. 作为参考,我在main.js中将nodeIntegration设置为true。

webPreferences: {
  preload: path.join(__dirname, 'preload.js'),
  nodeIntegration: true
}

Why would it allow me to use require() in a script on two html files, but not this new one? 为什么要允许我在两个HTML文件的脚本中使用require(),但不能在这个新文件中使用?

As zero298 mentioned electron suggests using a webview, which I may move to in the future. 正如Zero298所提到的,电子建议使用Web视图,我将来可能会使用它。

For now I ended up using window.parent to access the element that I needed in the parent window without using jQuery: 现在,我最终使用window.parent在不使用jQuery的情况下访问父窗口中所需的元素:

var but = window.parent.document.getElementById('saveAction');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM