Electron的第一个应用

      目前一个项目的Client包括移动端(Android,IOS)、web端还有桌面应用,作为一个会Html+css的开发人员来说,使用bootstrap作为前台的框架可以完美的解决web端和移动端的问题,但是对于桌面应用,心里有点不甘,因为想做出一个简单的客户端,你要么使用java的Swing编程,要么会使用MFC等等,这样学习的代价太高,也不便维护,于是Electron解决了这一问题,它可以将你做的web界面直接移植到桌面应用里。像现在比较火的钉订客户端就是使用这种方式开发,当我知道钉订客户端是使用html+css开发的时候,我的表情是酱紫的

当然了它是使用nw.js并不是electron,我们项目以后要使用Electron来开发桌面应用,那么就赶快制作我的第一个application---‘Hello world’;

Electron 框架的前身是 Atom Shell,可以让你写使用 JavaScript,HTML 和 CSS 构建跨平台的桌面应用程序。它是基于io.js 和 Chromium 开源项目,并用于在 Atom 编辑器中。Electron 是开源的,由 GitHub 维护,有一个活跃的社区。最重要的是,Electron 应用服务构建和运行在 Mac,Windows 和 Linux。

基于本人的情况,推荐下载Electron Releases来完成环境的支持,点击下载



也许你会下载的很慢很慢,分享一个百度云网盘http://pan.baidu.com/s/1b31GdC

下载解压,打开目录,运行electron.exe,弹出界面

环境什么的我们就不用担心了,如果你执意想通过npm来下载依赖包并运行的,会出现意想不到的错误,什么错误?亲自去跳!

现在来跑第一个应用

首先开看下他的目录结构

index.html---需要显示的页面,里面输出“hello world"

<span style="font-family:Microsoft YaHei;font-size:14px;"><!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    <!-- All of the Node.js APIs are available in this renderer process. -->
    We are using node <script>document.write(process.versions.node)</script>,
    Chromium <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>


</html>
</span>

main.js---负责Electron界面的启动与一些设置,比如工具栏啊,主题啦等等

<span style="font-family:Microsoft YaHei;font-size:14px;">const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

// 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 mainWindow

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.
  mainWindow.loadURL(`file://${__dirname}/index.html`)

  // Open the DevTools.
  //mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // 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.
    mainWindow = 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', function () {
  // On OS X 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', function () {
  // On OS X 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 (mainWindow === 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.
</span>

package.json---负责app的版本声明

<span style="font-family:Microsoft YaHei;font-size:14px;">{
  "name": "ElectronApplication",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js"
}
</span><span style="font-family:Microsoft YaHei;font-size:14px;">
</span>
将项目直接拖拉到Electron.exe界面里,应用就跑了起来

强烈推荐使用这种方式,说多了都是泪啊!

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,下面是一个简单的 Electron 应用程序示例,你可以在本地尝试运行它: 首先,创建一个新的文件夹,将其命名为 my-electron-app。然后在该文件夹中创建一个 package.json 文件,并将以下内容添加到文件中: ```json { "name": "my-electron-app", "version": "1.0.0", "description": "My first Electron app", "main": "main.js", "scripts": { "start": "electron ." }, "dependencies": { "electron": "^12.0.0" } } ``` 在上面的 package.json 文件中,我们指定了应用程序的名称、版本、描述和入口文件(main.js)。同时,我们还指定了要安装的依赖项(electron)以及启动应用程序的脚本("start": "electron .")。 接下来,在 my-electron-app 文件夹中创建一个名为 main.js 的文件,并将以下内容添加到文件中: ```js const { app, BrowserWindow } = require('electron'); function createWindow() { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }); win.loadFile('index.html'); win.webContents.openDevTools(); } app.whenReady().then(() => { createWindow(); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); }); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); ``` 在上面的 main.js 文件中,我们使用 require 引入了 Electron 的 app 和 BrowserWindow 模块。然后,我们定义了一个 createWindow 函数,该函数创建了一个新的浏览器窗口,并加载了 index.html 文件。最后,我们使用 app 模块的 whenReady 方法来创建应用程序窗口,并在窗口关闭时退出应用程序。 接下来,在 my-electron-app 文件夹中创建一个名为 index.html 的文件,并将以下内容添加到文件中: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My Electron App</title> </head> <body> <h1>Hello, Electron!</h1> </body> </html> ``` 在上面的 index.html 文件中,我们创建了一个简单的 HTML 页面,其中包含一个标题。 最后,打开命令行窗口,导航到 my-electron-app 文件夹,并运行以下命令来启动应用程序: ```bash npm start ``` 这将启动 Electron 应用程序,并在浏览器窗口中显示 "Hello, Electron!"。 注意:在上面的示例中,我们启用了 Node.js 集成,这是一个不安全的做法。实际开发中,你应该禁用 Node.js 集成,并使用预加载脚本来访问 Node.js API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值