博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Electron.js简介-第1部分:设置
阅读量:2506 次
发布时间:2019-05-11

本文共 6288 字,大约阅读时间需要 20 分钟。

In this article we’ll be learning how to develop our own native desktop applications using Electron.js. We’ll be making a basic todo list app to master the fundamentals behind creating menus, working with local data, and bundling our apps into something we can run on our local machine. If you ever wish to explore any of these options deeper, I would take a look at the .

在本文中,我们将学习如何使用Electron.js开发我们自己的本机桌面应用程序。 我们将制作一个基本的待办事项列表应用程序,以掌握创建菜单,使用本地数据以及将应用程序捆绑到可以在本地计算机上运行的东西之后的基础知识。 如果您想更深入地探索这些选项中的任何一个,我将看一下 。

先决条件 (Prerequisites)

Only some Node.js basics are necessary, but since we’ll be using Nedb, which is built on MongoDB and Mongoose, being familiar with using a NoSQL database will be very helpful, which you can .

仅需要一些Node.js基础知识,但是由于我们将使用基于MongoDB和Mongoose构建的Nedb,因此熟悉使用NoSQL数据库将非常有帮助,您可以 。

安装 (Installation)

We only need a few things to get things started.

我们只需要几件事就可以开始。

  • A version of for live reloading electron apps.

    实时重新加载电子应用程序的版本。

  • A version of MongoDB and Mongoose that allows us to save data directly to our machine.

    MongoDB和Mongoose的一个版本,使我们可以将数据直接保存到我们的计算机中。

  • A builder for our app so we can send and download it later.

    我们的应用程序的构建器,因此我们可以稍后发送和下载。

$ npm i electron electromon nedb electron-packager

档案结构 (File Structure)

All we really need to get started is a place to put our icons for our different operating systems, a HTML page with a client-side JavaScript file, and a file for our server. We’ll also break our Menu Bar into it’s own component since it can get a bit cumbersome.

我们真正需要入门的只是一个地方,可以放置我们用于不同操作系统的图标,带有客户端JavaScript文件HTML页面以及用于服务器的文件。 我们还会将菜单栏分解为它自己的组件,因为它可能会变得有些麻烦。

* assets 📂  * icons 📂 // electron-packager needs a different logo format for every platform    * mac  // mac and linux, since they take the same file type    * win * src 📂   * MenuBar.js  * index.html   * script.js* app.js* package.json

建立 (Setup)

For our project’s , the scripts object may seem complicated, but we’re just adding a start script and some build commands for when we’re ready to deploy to various platforms. In the build commands, make sure the --icon path leads to the correct location and ProductName equals the name of your app, we’ll worry about the icons later.

对于我们项目的 ,脚本对象可能看起来很复杂,但是我们只是在准备好将其部署到各种平台时添加一个启动脚本和一些构建命令。 在构建命令中,确保--icon路径指向正确的位置,并且ProductName等于您的应用程序的名称,稍后我们将担心这些图标。

package.json
package.json
{  "dependencies": {    "electromon": "^1.0.10",    "electron": "^5.0.8",    "electron-packager": "^14.0.3",    "nedb": "^1.8.0"  },  "name": "electron-app",  "version": "1.0.0",  "main": "app.js",  "devDependencies": {},  "scripts": {    "start": "electromon .",    "package-mac": "electron-packager . --overwrite --platform=darwin --arch=x64 --icon=assets/icons/mac/icon.icns --prune=true --out=release-builds",    "package-win": "electron-packager . --overwrite --asar=true --platform=win32 --arch=ia32 --icon=assets/icons/win/icon.ico --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron App\"",    "package-linux": "electron-packager . --overwrite --asar=true --platform=linux --arch=x64 --icon=assets/icons/mac/icon.icns --prune=true --out=release-builds"  },  "author": "",  "license": "ISC",  "description": ""}


Let’s get our interface set up with just a basic HTML page. I will be using the framework to make it look a little nicer. All we really need is a form to enter our new items, and and empty ul to append them onto later.

让我们通过一个基本HTML页面设置我们的界面。 我将使用框架使它看起来更好一些。 我们真正需要的只是一个表格来输入我们的新项目,然后清空ul以将其追加到以后。

index.html
index.html
  
Electron

    To avoid getting an error, we’re just going to set our menu bar to an empty array, and it’ll be passed to and rendered by app.js. We’ll go into more detail on that later.

    为了避免出错,我们将菜单栏设置为一个空数组,并将其传递给app.js并由app.js呈现。 稍后我们将对此进行详细介绍。

    MenuBar.js
    MenuBar.js
    const menuBar = [];module.exports = menuBar;

    窗口 (Window)

    After requiring everything, we’re going to destructure all of the tools we need off of electron.

    在需要所有东西之后,我们将使用电子来破坏我们需要的所有工具。

    • app Controls the lifecycle of our app; whether it’s on, shutting off, reloading, etc.

      app控制我们应用程序的生命周期; 是否开启,关闭,重新加载等

    • BrowserWindow Establishes the window itself, it comes with but all we really need is to tell it to use Node.js on our client side.

      BrowserWindow建立窗口本身,它带有但是我们真正需要的只是告诉它在客户端使用Node.js。

    • Menu Sets our menu bar from our template over in MenuBar.js

      MenuMenuBar.js通过模板设置菜单栏

    • ipcMain Controls event calls between the server and the client. It’s client-side counterpart, ipcRenderer, sends our data to our server while ipcMain will catch that before we can save the data to our machine.

      ipcMain控制服务器和客户端之间的事件调用。 它是客户端对应的ipcRenderer ,将我们的数据发送到我们的服务器,而ipcMain会在将数据保存到我们的机器之前捕获到该数据。

    Since we are essentially just making a web page we can just pass our new window our page URL, which we can make a bit more palatable using .

    由于本质上是制作网页,因此我们可以将页面URL传递给新窗口,使用可以使页面URL更具可口性。

    app.js
    app.js
    const electron = require('electron');const url = require('url');const path = require('path');// Componentsconst menuBar = require('./src/components/MenuBar');const { app, BrowserWindow, Menu, ipcMain } = electron;let mainWindow;app.on('ready', () => {  mainWindow = new BrowserWindow({    webPreferences: { nodeIntegration: true } // This will allow us to use node specific tools like require in our client side javascript.  });  mainWindow.loadURL(url.format({    // All this is doing is passing in to file://your_directory/src/index.html    pathname: path.join(__dirname, 'src/index.html'),    protocol: 'file:',    slashes: true  }));  // Shut down app when window closes  mainWindow.on('closed', () => app.quit());   const mainMenu = Menu.buildFromTemplate(menuBar);  Menu.setApplicationMenu(mainMenu);});

    结论 (Conclusion)

    Since this article started to get excessively long, I decided to break it into two parts to make it a bit more graspable. What we have so far makes a good boilerplate and In we’ll be going over the more interesting bits like communicating between the client and the server, managing our database, and bundling our app. You can find the repo for the completed example .

    由于本文开始变得太长,我决定将其分为两部分,以使其更加易懂。 到目前为止,我们所拥有的东西是一个很好的样板,在第二我们将介绍一些更有趣的内容,例如客户端和服务器之间的通信,管理数据库以及捆绑应用程序。 您可以在找到完整示例的 。

    翻译自:

    转载地址:http://pphgb.baihongyu.com/

    你可能感兴趣的文章
    MapReduce的 Speculative Execution机制
    查看>>
    大数据学习之路------借助HDP SANDBOX开始学习
    查看>>
    Hadoop基础学习:基于Hortonworks HDP
    查看>>
    为什么linux安装程序 都要放到/usr/local目录下
    查看>>
    Hive安装前扫盲之Derby和Metastore
    查看>>
    永久修改PATH环境变量的几种办法
    查看>>
    大数据学习之HDP SANDBOX开始学习
    查看>>
    Hive Beeline使用
    查看>>
    Centos6安装图形界面(hdp不需要,hdp直接从github上下载数据即可)
    查看>>
    CentOS7 中把yum源更换成163源
    查看>>
    关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
    查看>>
    2020-11-18
    查看>>
    Docker面试题(二)
    查看>>
    【NOI 2018】归程(Kruskal重构树)
    查看>>
    注册用户
    查看>>
    TZC Intercommunication System
    查看>>
    HDU 4571 SPFA+DP
    查看>>
    centos 创建以日期为名的文件夹
    查看>>
    Java Timer触发定时器
    查看>>
    Page Object设计模式
    查看>>