[electron]官方示例解析

官方例子


github链接

main.js

js 复制代码
const { app, BrowserWindow } = require('electron')

说句实话这里的语法是有部分看不懂的。导入模块虽然electron有很多模块。但是这里只是用到了app 和 BrowserWindow

js 复制代码
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文件

js 复制代码
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()函数。

js 复制代码
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程序了。

相关推荐
不要试图纠正别人4 天前
178 个 IPC 通道不可怕,可怕的是 preload 里那一行
前端·electron
不可能片场6 天前
工程实践 雪花id进JS别当数字传
前端·electron
鶴哥只手遮天6 天前
# 《Electron 45.2.3 内核解剖》第 6 章 Node.js 集成:两个事件循环的婚姻
前端·electron·开源
鶴哥只手遮天7 天前
《Electron 45.2.3 内核解剖》第 4 章 补丁体系:380 个补丁改了什么
前端·electron
北城笑笑10 天前
Vue 109 ,从零搭建 Vue3 + Electron 工程:生态、安全桥接与打包选型( Vue + Electron )
前端·javascript·vue.js·安全·electron·vue
RobinDevNotes12 天前
从 Electron 到 GPUI,9 个跨平台桌面方案该怎么选
前端·javascript·rust·electron·openresty·前端开发工具
不可能片场15 天前
内容运营自动化 配图折行别把词切两半
前端·electron
黄连木笛15 天前
不改一行 Postman 源码,把它汉化了:Electron preload 注入 + fetch 拦截实战
javascript·electron
不可能片场16 天前
Electron 发布 正文写入的三条通道与读回口径
前端·electron
不可能片场17 天前
AI视频成片 帧间差分把拼接缝照出来
前端·electron