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..)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)