問題描述
為了學習新的 ES6 語法,我一直在嘗試重構一些 JS 代碼.
To learn the new ES6 syntax, I've been trying to refactor some JS code.
我對整個導入/導出方法感到非常困惑.
I'm absolutely confused though by the whole import / export methods.
如何將這個 require
語句改成 ES6?
How do I change this require
statement into ES6?
var remote = require('electron').remote
我看過這個答案但是:
- 它不起作用
- 看起來并沒有太多 ES6 風格
有什么想法嗎?
推薦答案
似乎在 Node 6 或 Chrome 51 中 導入都沒有實現,所以 Electron 也不支持它們,根據這篇文章:https://discuss.atom.io/t/does-electron-support-es6/19366/18
It seems imports are not implemented in either Node 6 or Chrome 51 so Electron also does not support them, according to this post: https://discuss.atom.io/t/does-electron-support-es6/19366/18
最后一個電子文檔也不使用導入,它們使用 destructuring 語法:
And also the last electron doc doesn't use imports, they use destructuring syntax:
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/
But you can use babel with the require hook: http://babeljs.io/docs/usage/require/
自動編譯每個必需的模塊,以便您可以使用導入.當然,給電子的腳本(需要 babel 的腳本)沒有編譯,所以你需要做一個引導程序:
To be auto compile each required modules so you will be able to use imports. Of course the script given to electron (the one that require babel) is not compiled so you need to make a bootstrap:
// bootwithbabel.js
require("babel-register");
require( process.argv.splice(2) );
在外殼(sh)中:
electron bootwithbabel.js app.es
alias electrones="electron bootwithbabel.js "
electrones coron.es // ^^
然后你可以在你的應用中寫:
Then in your app you can then write:
import electron from 'electron';
import { remote } from 'electron';
你也可以只導入遠程模塊:
You can also import only the remote module:
import { remote } from 'electron';
但你只能在一個語句中同時導入:
But you can only import both in one statement:
import electron, { remote } from 'electron'
electron.ipcRenderer.on();
let win = new remote.BrowserWindow({width: 800, height: 600});
remote.getGlobal(name)
游樂場
這篇關于ES6 語法導入 Electron (require..)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!