初学electron

接触了两周的electron,感觉还不错,以后pc端基本上可以用electron加壳写pc端应用了,可以用nodejs的模块,也可以用es6、7,还可以直接操作系统文件。基本上可以完成大多数pc应用了。

就是安装慢成狗了。。。。用镜也卡!

快速开始

Electron通过为运行时提供丰富的本机(操作系统)API,可以使用纯JavaScript创建桌面应用程序。您可以将其看作是Node.js运行时的一种变体,它专注于桌面应用程序而不是Web服务器。

这并不意味着Electron是JavaScript绑定到图形用户界面(GUI)库。相反,Electron使用网页作为其GUI,因此您也可以将其视为由JavaScript控制的最小的Chromium浏览器。

主要过程

在电子,在运行过程中package.jsonmain脚本被称为主处理。在主进程中运行的脚本可以通过创建网页来显示GUI。

渲染器过程

由于Electron使用Chromium来显示网页,因此也使用了Chromium的多进程架构。Electron中的每个网页都在其自己的进程中运行,这称为渲染器进程

在正常的浏览器中,网页通常在沙箱环境中运行,并且不允许访问本机资源。然而,电子用户有能力在网页中使用Node.js API,允许较低级别的操作系统交互。

主进程和渲染器进程之间的差异

主进程通过创建BrowserWindow实例来创建网页。每个BrowserWindow实例在其自己的渲染器进程中运行网页。当BrowserWindow实例被销毁时,相应的渲染器进程也被终止。

主进程管理所有网页及其相应的渲染器进程。每个渲染器进程是隔离的,只关心在其中运行的网页。

在网页中,不允许调用本地GUI相关的API,因为管理网页中的本地GUI资源是非常危险的,并且容易泄漏资源。如果要在Web页面中执行GUI操作,则Web页面的渲染器进程必须与主进程通信,以请求主进程执行这些操作。

在Electron中,我们有几种方法在主进程和渲染器进程之间进行通信。Like ipcRendereripcMain用于发送消息的模块,以及用于RPC样式通信的远程模块。还有一个关于如何在网页之间共享数据的常见问题条目。

写你的第一电子应用程序

通常,Electron应用程序的结构如下:

your-app/
├── package.json
├── main.js
└── index.html

其格式与package.jsonNode模块的格式完全相同,main字段指定的脚本是应用程序的启动脚本,它将运行主进程。您的示例package.json可能如下所示:

{
"name" : "your-app",
"version" : "0.1.0",
"main" : "main.js"
}

注意:如果main字段不存在package.json,Electron将尝试加载index.js

main.js应创建窗口和处理系统事件,一个典型的例子是:

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url') // Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: , height: }) // and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
})) // Open the DevTools.
win.webContents.openDevTools() // Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
} // This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow) // Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
}) app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
}) // In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

最后index.html是您要显示的网页:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>
</html>

运行您的应用程序

一旦你创建了初始main.jsindex.htmlpackage.json文件,你可能想尝试在本地运行你的应用程序来测试它,并确保它的工作正常。

electron

electronnpm包含Electron的预编译版本的模块。

如果您已经在全局安装npm,那么您只需要在应用程序的源目录中运行以下命令:

electron .

如果您已在本地安装,请运行:

macOS / Linux

$ ./node_modules/.bin/electron .

视窗

$ .\node_modules\.bin\electron .

手动下载的电子二进制

如果您手动下载了电子邮件,您还可以使用包含的二进制文件直接执行您的应用程序。

视窗

$ .\electron\electron.exe your-app\

Linux

$ ./electron/electron your-app/

macOS

$ ./Electron.app/Contents/MacOS/Electron your-app/

Electron.app这里是Electron的发行包的一部分,你可以从这里下载。

作为分发版运行

完成编写应用程序后,您可以按照应用程序分发指南创建分发,然后再执行打包的应用程序。

试试这个例子

使用存储electron/electron-quick-start库克隆并运行本教程中的代码。

注意:运行此操作需要在系统上使用Git和Node.js(其中包括npm)。

# Clone the repository
$ git clone https://github.com/electron/electron-quick-start
# Go into the repository
$ cd electron-quick-start
# Install dependencies
$ npm install
# Run the app
$ npm start

electron快速开始的更多相关文章

  1. Electron快速入门之事件

    const { app, BrowserWindow } = require('electron') function createWindow () {   const win = new Brow ...

  2. Electron快速入门之debug

    view->toggleDevelpper Tools 本地桌面调试 浏览器debug "start": "electron --inspect=5858 .&qu ...

  3. Electron快速入门

    node -v npm -v 安装node环境 my-electron-app/ ├── package.json ├── main.js └── index.html 为您的项目创建一个文件夹并安装 ...

  4. Electron 快速入门

    https://www.w3cschool.cn/electronmanual/p9al1qkx.html

  5. (译)通过 HTML、JS 和 Electron 创建你的第一个桌面应用

    原文:Creating Your First Desktop App With HTML, JS and Electron 作者:Danny Markov 近年来 web 应用变得越来越强大,但是桌面 ...

  6. 使用electron开发一个h5的客户端应用创建http服务模拟后台接口mock

    使用electron开发一个h5的客户端应用创建http服务模拟后端接口mock 在上一篇<electron快速开始>里讲述了如何快速的开始一个electron的应用程序,既然electr ...

  7. 安装使用electron辛路历程

    安装使用electron辛路历程 成功安装electron以及成功使用第一个应用,整整花费了我一整天的时间,各种百度,各种尝试.最终,终于总结了一个亲测可行的终极可执行方案: electron 简单介 ...

  8. 初探Electron,从入门到实践

    本文由葡萄城技术团队于博客园原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者.   在开始之前,我想您一定会有这样的困惑:标题里的Electron ...

  9. electron客户端开发

    如何新建一个 Electron 项目? electron快速入门笔记: https://www.jianshu.com/p/f134878af30f 然后自己新建一个 Electron 项目,在项目中 ...

随机推荐

  1. 面向对象程序设计_Task5_Calculator1.5.0

    The 3rd part of the Calculator program _ FILE I/O 题目链接:第五次作业(计算器第三步) github链接:Calculator_1.5.0 第五次作业 ...

  2. Spring MVC Interceptor

    1 在spring-servlet.xml中进行如下配置 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path=& ...

  3. bzoj1808 [Ioi2007]training 训练路径

    Description 马克(Mirko)和斯拉夫克(Slavko)正在为克罗地亚举办的每年一次的双人骑车马拉松赛而紧张训练.他们需要选择一条训练路径. 他们国家有N个城市和M条道路.每条道路连接两个 ...

  4. oc的静态函数static

    oc的静态函数与类函数不同: 1.静态函数与c++中表现一致,只在模块内部可见: 2.静态函数内部没有self变量: 3.静态函数不参与动态派发:没有在函数列表里:是静态绑定的: @implement ...

  5. 七:Java之封装、抽象、多态和继承

    本文章介绍了关于Java中的面向对象封装.抽象.继承.多态特点 Java面向对象主要有四大特性:封装.抽象.继承和多态. 一.封装 封装就是将抽象得到的数据和行为(或功能)相结合,形成一个有机的总体, ...

  6. [BJWC2011]最小三角形

    嘟嘟嘟 这一看就是平面分治的题,所以就想办法往这上面去靠. 关键就是到\(mid\)点的限制距离是什么.就是对于当前区间,所有小于这个距离的点都选出来,参与更新最优解. 假设从左右区间中得到的最优解是 ...

  7. node.js 连接 sql server 包括低版本的sqlserver 2000

    利用tedious连接,github地址:https://github.com/tediousjs/tedious 废话不多时直接上代码. connection.js var Connection = ...

  8. 【转】Android 4.0 Launcher2源码分析——启动过程分析

    Android的应用程序的入口定义在AndroidManifest.xml文件中可以找出:[html] <manifest xmlns:android="http://schemas. ...

  9. HDU1047(多个大数相加)

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  10. Using 1.7 requires compiling with Android 4.4 (KitKat); currently using

    今天编译一个project,我设置为api 14,可是编译报错: Using 1.7 requires compiling with Android 4.4 (KitKat); currently u ...