electron快速开始

    xiaoxiao2021-03-26  5

    初学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: 800, height: 600}) // 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

     

    转载请注明原文地址: https://ju.6miu.com/read-600226.html

    最新回复(0)