2017-02-19 62 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

相關問題