[英]ES6 syntax import Electron (require..)
為了學習新的 ES6 語法,我一直在嘗試重構一些 JS 代碼。
盡管我對整個導入/導出方法感到非常困惑。
如何將此require
語句更改為 ES6?
var remote = require('electron').remote
我看過這個答案,但是:
有什么想法嗎?
根據這篇文章,似乎在 Node 6 或 Chrome 51 中都沒有實現導入,所以 Electron 也不支持它們: https : //discuss.atom.io/t/does-electron-support-es6/19366/18
而且最后一個電子文檔不使用導入,它們使用解構語法:
const { BrowserWindow } = require('electron').remote
// or
const { remote } = require('electron')
const { BrowserWindow } = remote
http://electron.atom.io/docs/api/remote/
但是你可以使用帶有 require 鈎子的 babel: http : //babeljs.io/docs/usage/require/
要自動編譯每個必需的模塊,以便您能夠使用導入。 當然,給電子的腳本(需要 babel 的那個)沒有編譯,所以你需要做一個引導程序:
// bootwithbabel.js
require("babel-register");
require( process.argv.splice(2) );
在外殼(sh)中:
electron bootwithbabel.js app.es
alias electrones="electron bootwithbabel.js "
electrones coron.es // ^^
然后在您的應用程序中,您可以編寫:
import electron from 'electron';
import { remote } from 'electron';
您也可以只導入遠程模塊:
import { remote } from 'electron';
但是您只能在一個語句中導入兩者:
import electron, { remote } from 'electron'
electron.ipcRenderer.on();
let win = new remote.BrowserWindow({width: 800, height: 600});
remote.getGlobal(name)
盡管我對整個導入/導出方法感到非常困惑。
混合不同的模塊系統確實會令人困惑。
- 它不起作用
const electron = require('electron');
const remote = electron.remote;
和你擁有的完全一樣
var remote = require('electron').remote
如果你的工作,另一個也會。 但是,我只會堅持你的。
- 它似乎並不像 ES6 風格
誰在乎? Node 本身不支持 ES6 imports
和exports
並且還不清楚 CommonJS 模塊應該如何映射到 ES6 模塊。 如果您只是為 Node 編寫代碼,我建議您堅持使用require
。
你可以嘗試做
import electron from 'electron';
const {remote} = electron;
如今,每個版本的 Electron 都帶有基本的打字稿支持。 因此,如果您在項目中使用 TS 或 TSX 文件,那么您可以在該文件中使用 ES 導入語句。 是否使用 ES 模塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.