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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35374353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:39:14  来源:igfitidea点击:

ES6 syntax import Electron (require..)

javascriptimportecmascript-6electron

提问by c1c1c1

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 requirestatement into ES6?

如何将此require语句更改为 ES6?

var remote = require('electron').remote

I've seen this answerbut:

我看过这个答案,但是:

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

Any thoughts?

有什么想法吗?

回答by Thomas Di G

It seems imports are not implementedin 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 destructuringsyntax:

而且最后一个电子文档不使用导入,它们使用解构语法:

const { BrowserWindow } = require('electron').remote
// or
const { remote } = require('electron')
const { BrowserWindow } = remote

http://electron.atom.io/docs/api/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

操场

回答by Felix Kling

I'm absolutely confused though by the whole import / export methods.

尽管我对整个导入/导出方法感到非常困惑。

Mixing different module systems can indeed be confusing.

混合不同的模块系统确实会令人困惑。

  1. It doesn't work
  1. 它不起作用
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
  1. 它似乎并不像 ES6 风格

Who cares? Node doesn't support ES6 importsand exportsnatively and it's not super clear how CommonJS modules should map to ES6 modules. I recommend to stick with requireif you are only writing for Node anyway.

谁在乎?Node 不支持 ES6importsexports本机,并且还不清楚 CommonJS 模块应该如何映射到 ES6 模块。require如果您只是为 Node 编写代码,我建议您坚持使用。



You could tryto do

你可以尝试

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