electron快速开始
初学electron
接触了两周的electron,感觉还不错,以后pc端基本上可以用electron加壳写pc端应用了,可以用nodejs的模块,也可以用es6、7,还可以直接操作系统文件。基本上可以完成大多数pc应用了。
就是安装慢成狗了。。。。用镜也卡!
快速开始
Electron通过为运行时提供丰富的本机(操作系统)API,可以使用纯JavaScript创建桌面应用程序。您可以将其看作是Node.js运行时的一种变体,它专注于桌面应用程序而不是Web服务器。
这并不意味着Electron是JavaScript绑定到图形用户界面(GUI)库。相反,Electron使用网页作为其GUI,因此您也可以将其视为由JavaScript控制的最小的Chromium浏览器。
主要过程
在电子,在运行过程中package.json的main脚本被称为主处理。在主进程中运行的脚本可以通过创建网页来显示GUI。
渲染器过程
由于Electron使用Chromium来显示网页,因此也使用了Chromium的多进程架构。Electron中的每个网页都在其自己的进程中运行,这称为渲染器进程。
在正常的浏览器中,网页通常在沙箱环境中运行,并且不允许访问本机资源。然而,电子用户有能力在网页中使用Node.js API,允许较低级别的操作系统交互。
主进程和渲染器进程之间的差异
主进程通过创建BrowserWindow实例来创建网页。每个BrowserWindow实例在其自己的渲染器进程中运行网页。当BrowserWindow实例被销毁时,相应的渲染器进程也被终止。
主进程管理所有网页及其相应的渲染器进程。每个渲染器进程是隔离的,只关心在其中运行的网页。
在网页中,不允许调用本地GUI相关的API,因为管理网页中的本地GUI资源是非常危险的,并且容易泄漏资源。如果要在Web页面中执行GUI操作,则Web页面的渲染器进程必须与主进程通信,以请求主进程执行这些操作。
在Electron中,我们有几种方法在主进程和渲染器进程之间进行通信。Like ipcRenderer和ipcMain用于发送消息的模块,以及用于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.js,index.html和package.json文件,你可能想尝试在本地运行你的应用程序来测试它,并确保它的工作正常。
electron
electron是npm包含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快速开始的更多相关文章
- Electron快速入门之事件
const { app, BrowserWindow } = require('electron') function createWindow () { const win = new Brow ...
- Electron快速入门之debug
view->toggleDevelpper Tools 本地桌面调试 浏览器debug "start": "electron --inspect=5858 .&qu ...
- Electron快速入门
node -v npm -v 安装node环境 my-electron-app/ ├── package.json ├── main.js └── index.html 为您的项目创建一个文件夹并安装 ...
- Electron 快速入门
https://www.w3cschool.cn/electronmanual/p9al1qkx.html
- (译)通过 HTML、JS 和 Electron 创建你的第一个桌面应用
原文:Creating Your First Desktop App With HTML, JS and Electron 作者:Danny Markov 近年来 web 应用变得越来越强大,但是桌面 ...
- 使用electron开发一个h5的客户端应用创建http服务模拟后台接口mock
使用electron开发一个h5的客户端应用创建http服务模拟后端接口mock 在上一篇<electron快速开始>里讲述了如何快速的开始一个electron的应用程序,既然electr ...
- 安装使用electron辛路历程
安装使用electron辛路历程 成功安装electron以及成功使用第一个应用,整整花费了我一整天的时间,各种百度,各种尝试.最终,终于总结了一个亲测可行的终极可执行方案: electron 简单介 ...
- 初探Electron,从入门到实践
本文由葡萄城技术团队于博客园原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 在开始之前,我想您一定会有这样的困惑:标题里的Electron ...
- electron客户端开发
如何新建一个 Electron 项目? electron快速入门笔记: https://www.jianshu.com/p/f134878af30f 然后自己新建一个 Electron 项目,在项目中 ...
随机推荐
- Shell定时删除日志
vim del_log.sh #!/bin/bash location="/home/dl/code/logs" find $location -mtime +4 -type f ...
- mysql 索引分类以及用途分析
MySQL索引分为普通索引.唯一性索引.全文索引.单列索引.多列索引等等.这里将为大家介绍着几种索引各自的用途. 一. MySQL: 索引以B树格式保存 Memory存储引擎可以选择Hash或BTre ...
- rimraf命令 递归删除目录所有文件
使用webpack build文件项目时每次都会生成一个dist目录,有时需要把dist目录里的所以旧文件全部删掉, 除了可以使用rm -rf /dist/命令删除外,还可以使用rimraf /dis ...
- 【python库安装问题解决】UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 121: invalid start byte
好久没用python了...今天随便pip安装个库突然报错: Exception:‘’ (most recent call last): File "C:\ProgramData\Anac ...
- 1070. [SCOI2007]修车【费用流】
Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同 的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序, ...
- Installing Oracle Database 12c Release 2(12.2) RAC on RHEL7.3 in Silent Mode
概要 在RHEL7静默方式安装oracle database 12.2 RAC. 一.环境配置 1. 配置hosts文件 cp /etc/hosts /etc/hosts_$(date +%Y%d%m ...
- Java并发编程 -- 文章汇总
文章汇总 1.Thread和Runnable 2.synchronized 3.Lock 4.Executor框架 5.信号量和障碍器 6.Exchanger线程间交换数据 7.Java内存操作总结
- Linux MySql5.6.38安装过程
1.下载mysql安装包mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz 2.用xftp工具将其上传到Linux服务器上的soft文件夹,没有的话先创建 [root ...
- ZOJ 4103 浙江省第16届大学生程序设计竞赛 D题 Traveler 构造
这个题,正赛的时候也没有过,不过其实已经有了正确的解法,可惜时间不多了,就没有去尝试. 题意是有n个点,i点能通向i-1,然后i和i*2.i*2+1互通. 请你构造一种路径从1能走完所有点,并且不重复 ...
- (转)WebSocket的原理
前言:无聊逛知乎,就逛到H5的栏目去了,正好看到了关于Websocket的东西.个人是比较喜欢看这类风格的,转到博客分享,以便自己以后理解. ---------------------分割线----- ...