[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程序了。

相关推荐
CDwenhuohuo14 小时前
electron pc项目打包成桌面端
前端·javascript·electron
不可能片场2 天前
命令行中文变问号 我用环境变量救了场
前端·electron
不可能片场2 天前
AI视频口型对齐 用音频驱动说话镜头
前端·electron
Zoom3 天前
【开源】7 天,我用 Rust 复刻了 FinalShell!Rhost v1.0.0 发布
rust·electron·shell
爱丶不疚4 天前
Electron: Sentry 都采集了什么?你知道吗
前端·electron
格数致用4 天前
03- Luckysheet与Vue3+Electron集成实战指南
electron·vue3·luckysheet
fellow996 天前
Electron + pnpm 在鸿蒙应用里跑起来
electron·harmonyos·openharmony·deepseek·deepseekharness
记得开心一点嘛6 天前
Trellora:基于 Electron、React 和本地知识库的 AI 知识工作台
人工智能·react.js·electron
500846 天前
React Native for OpenHarmony 实战:三方库 react-native-url-polyfill 的鸿蒙化适配指南
javascript·react native·react.js·性能优化·electron·harmonyos
不可能片场6 天前
内容运营自动化 用相似度拦住撞车选题
前端·electron