[electron]官方示例解析

官方例子

在这里插入图片描述
github链接

main.js

const { app, BrowserWindow } = require('electron')

说句实话这里的语法是有部分看不懂的。导入模块虽然electron有很多模块。但是这里只是用到了appBrowserWindow
在这里插入图片描述

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

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

创建窗口函数,new了一个BrowserWindow。并指定了一些属性,然后加载html文件

app.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    // 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 (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app whenReady之后就可以创建窗口了,调用createWindow()函数。

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

当所有窗口退出后。app退出。

package.json

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "GitHub",
  "license": "CC0-1.0",
  "devDependencies": {
    "electron": "^29.0.0"
  }
}

这个文件可以由npm init自动生成。自动生成的文件需要将修改为

  "scripts": {
    "start": "electron ."
  }

然后就可以通过 vscode直接运行electron程序了。

相关推荐

  1. Android-UWB通信示例代码

    2024-03-18 00:00:05       63 阅读
  2. Electron桌面应用开发:从入门到发布全流程

    2024-03-18 00:00:05       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 00:00:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 00:00:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 00:00:05       82 阅读
  4. Python语言-面向对象

    2024-03-18 00:00:05       91 阅读

热门阅读

  1. ActiveMQ

    ActiveMQ

    2024-03-18 00:00:05      33 阅读
  2. 2024.03.17 校招 实习 内推 面经

    2024-03-18 00:00:05       37 阅读
  3. CSS中那些你不知道的选择器

    2024-03-18 00:00:05       37 阅读
  4. HJ14 字符串排序【C语言】

    2024-03-18 00:00:05       36 阅读
  5. 2024-3-17Go语言入门

    2024-03-18 00:00:05       37 阅读
  6. Python 3 教程(6)

    2024-03-18 00:00:05       34 阅读
  7. Compose UI 之 Buttons 按钮 & IconButtons 图标按钮

    2024-03-18 00:00:05       41 阅读
  8. 【Jetson Nano】jetson nano一些基本功能命令

    2024-03-18 00:00:05       34 阅读
  9. c++三分算法思想及实现方法

    2024-03-18 00:00:05       40 阅读
  10. 高质量 Git 仓库汇总(持续更新,方便查看)

    2024-03-18 00:00:05       38 阅读
  11. 169.多数元素

    2024-03-18 00:00:05       37 阅读
  12. python 基础练习题3

    2024-03-18 00:00:05       34 阅读