2017-02-19 57 views
0

我试图通过从主进程启动的HTTP服务器上提供HTML和渲染器JavaScript文件来制作我的Electron应用的移动版本。通过HTTP服务电子渲染器进程并使用require

我的渲​​染器进程JavaScript大量使用require。他们在Electron内部工作得很好。在电子以外,他们不会因为window.require没有被定义。

所以我试着重写一些代码,以便它可以与required或以前加载在<script>标记中的模块一起使用。

这里是我试过到目前为止:

// Initial situation: 
const $ = require('jQuery'); 
// on mobile : require is not defined 

// Try 1 
if (window.require) { 
    const $ = require('jQuery'); 
} 
// doesn't work: $ is only defined in the scope of the if 

// Try 2 
let $; 
if (window.require) { 
    $ = require('jQuery'); 
} 
// on mobile : $ was defined from jQuery being loaded from a script tag, 
// but I just overwrote it with undefined 

// Try 3 
let $; 
if (window.require) { 
    $ = require('jQuery'); 
} 
else { 
    $ = window.$; 
} 
// on mobile : window.$ is undefined 

任何人都可以看到我错过了什么?或者,也许只是告诉我如何在非Electron页面上设置require

回答

0

最后我做了以下内容:

if (window.require) { 
    window.$ = require('jQuery'); 
} 

,然后使用window.$代替$整个渲染过程。

+0

'window。$'和'$'在浏览器环境中被认为是相同的。 – KeitIG

+0

如果你在这个页面上打开devtools,'windows。$$$'是未定义的,'$$$'是一个ReferenceError。所以他们不完全等效。在我的情况下,window。$事先不存在,所以我必须在窗口中显式设置(或声明一个局部变量)。 – foucdeg

+0

哦,对,捆绑商不会认为它们是一样的 – KeitIG

相关问题