ES6 语法导入 Electron (require..)

ES6 syntax import Electron (require..)(ES6 语法导入 Electron (require..))
本文介绍了ES6 语法导入 Electron (require..)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

为了学习新的 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

我看过这个答案但是:

  1. 它不起作用
  2. 看起来并没有太多 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..)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)