[Electron] 引入 jQuery 之類的問題。

  • 346
  • 0

Electron (atom.io) 引入 jQuery 之類的問題。

在使用 electron (.atom.io) 的時候,如果是在 render process 的 js 裡面要使用 jquery,是另外一回事。如果是像我一樣,把原來是網頁程式改成 electron,在 html 裡面要引入 jquery 一樣會有符號問題,因為 electron 有去修改 DOM 裡面的變數。所以得要修正回來才行。

Electron (atom.io) 引入 jQuery 之類的問題。

在使用 electron (.atom.io) 的時候,如果是在 render process 的 js 裡面要使用 jquery,是另外一回事。如果是像我一樣,把原來是網頁程式改成 electron,在 html 裡面要引入 jquery 一樣會有符號問題,因為 electron 有去修改 DOM 裡面的變數。所以得要修正回來才行。
在網頁程式裡的最前面加入修正:


<head>
<script>
window.nodeRequire = require;
delete window.require;
delete window.exports;
delete window.module;
</script>
<script type="text/javascript" src="jquery.js">
</script>
</head>

在 main process 的 nodeIntegration 不關掉好像也沒有關係。現在我還沒有遇到問題。
參考:
https://electron.atom.io/docs/faq/…

2017.09.11 更新:
還有另外一種做法,我也比較喜歡。我就是不想去動原來網頁程式的內容的情況下,在 main.js (有寫的人應該知道這是什麼)裡面,createWindow 的地方,創建新的 BrowserWindow 時,加入新的選項 nodeIntegration: false,如下:

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow(
 {width: 800, height: 600,
 webPreferences: { //https://electron.atom.io/docs/faq/#i-can-not-use-jqueryrequirejsmeteorangularjs-in-electron
  nodeIntegration: false
  }
 })
  // and load the index.html of the app. 
  win.loadURL('http://127.0.0.1:10001/')
  // Open the DevTools.
  ////win.webContents.openDevTools()
  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}

這樣原來網頁程式就不用動了。

 

 

 

分享