简体   繁体   English

ES6 语法导入 Electron(需要..)

[英]ES6 syntax import Electron (require..)

To learn the new ES6 syntax, I've been trying to refactor some JS code.为了学习新的 ES6 语法,我一直在尝试重构一些 JS 代码。

I'm absolutely confused though by the whole import / export methods.尽管我对整个导入/导出方法感到非常困惑。

How do I change this require statement into ES6?如何将此require语句更改为 ES6?

var remote = require('electron').remote

I've seen this answer but:我看过这个答案,但是:

  1. It doesn't work它不起作用
  2. It doesn't really seems to be much ES6-sque它似乎并不像 ES6 风格

Any thoughts?有什么想法吗?

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根据这篇文章,似乎在 Node 6 或 Chrome 51 中都没有实现导入,所以 Electron 也不支持它们: https : //discuss.atom.io/t/does-electron-support-es6/19366/18

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/

But you can use babel with the require hook: http://babeljs.io/docs/usage/require/但是你可以使用带有 require 钩子的 babel: http : //babeljs.io/docs/usage/require/

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:当然,给电子的脚本(需要 babel 的那个)没有编译,所以你需要做一个引导程序:

// bootwithbabel.js
require("babel-register");
require( process.argv.splice(2) );

In shell (sh):在外壳(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)

playground 操场

I'm absolutely confused though by the whole import / export methods.尽管我对整个导入/导出方法感到非常困惑。

Mixing different module systems can indeed be confusing.混合不同的模块系统确实会令人困惑。

  1. It doesn't work它不起作用
const electron = require('electron');
const remote = electron.remote;

is exactly the same as what you have和你拥有的完全一样

var remote = require('electron').remote

If yours work, the other will as well.如果你的工作,另一个也会。 However, I would simply stick with yours.但是,我只会坚持你的。

  1. It doesn't really seems to be much ES6-sque它似乎并不像 ES6 风格

Who cares?谁在乎? Node doesn't support ES6 imports and exports natively and it's not super clear how CommonJS modules should map to ES6 modules. Node 本身不支持 ES6 importsexports并且还不清楚 CommonJS 模块应该如何映射到 ES6 模块。 I recommend to stick with require if you are only writing for Node anyway.如果您只是为 Node 编写代码,我建议您坚持使用require


You could try to do你可以尝试

import electron from 'electron';
const {remote} = electron;

These days every version of Electron comes with basic typescript support.如今,每个版本的 Electron 都带有基本的打字稿支持。 So if you use a TS or TSX file in your project -then you can use ES Import statements inside that file.因此,如果您在项目中使用 TS 或 TSX 文件,那么您可以在该文件中使用 ES 导入语句。 Whether you use an ES module or not.是否使用 ES 模块。

https://www.electronjs.org/blog/typescript https://www.electronjs.org/blog/typescript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM