electron 是什么?
electron是一个构建跨平台桌面应用的框架,开发者可以基于nodejs以及框架提供的调用系统组件的能力让html应用构建为桌面应用
基于vue-cli 打造一个electron项目
-
环境准备:建议提前准备 科学上网工具 / fastgithub 等
-
创建vue项目 vue create electron-vue
-
安装electron-builder工具 vue add electron-builder 注意此处为 vue add
-
如果没有科学上网工具,大概率会出现卡住不动的情况,耐心等待或者结束掉多试几次。
-
安装成功后会让你选择electron 的版本,选lts版,lts版自行去官网查询
-
如果自动安装没能成功,需要手动自行安装,先打开package.json,手动添加script、执行入口、开发依赖、
...
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
+ "electron:build": "vue-cli-service electron:build",
+ "electron:serve": "vue-cli-service electron:serve",
+ "postinstall": "electron-builder install-app-deps",
+ "postuninstall": "electron-builder install-app-deps"
},
+ "main": "background.js",
...
"devDependencies": {
+ "electron": "^13.0.0",
+ "vue-cli-plugin-electron-builder": "^2.1.1",
},
...
-
是手动安装,此时还缺少一个自动生成的background.js 在src目录下添加background.js
'use strict'
import { app, protocol, BrowserWindow, screen, Menu, globalShortcut } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
// import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])
async function createWindow () {
// Create the browser window.
Menu.setApplicationMenu(null) // 无菜单
const mainWindow = new BrowserWindow({
width: screen.getPrimaryDisplay().workAreaSize.width,
height: screen.getPrimaryDisplay().workAreaSize.height,
transparent: true, // 隐藏系统菜单栏
useContentSize: true,
resizable: false, // 禁止改变大小
// show: false,
// paintWhenInitiallyHidden: true,
frame: false, // 无边框窗口
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
}
})
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await mainWindow.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) mainWindow.webContents.openDevTools()
} else {
createProtocol('app')
// Load the index.html when not in development
mainWindow.loadURL('app://./index.html')
}
// 设置全屏
mainWindow.setFullScreen(true)
// mainWindow.once('ready-to-show', () => {
// mainWindow.show()
// })
globalShortcut.register('CmdOrCtrl+Shift+O', () => {
if (mainWindow.webContents.isDevToolsOpened()) {
mainWindow.webContents.closeDevTools()
} else {
mainWindow.webContents.openDevTools({ mode: 'right' })
}
})
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS 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', () => {
// 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()
})
// 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', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
// try {
// await installExtension(VUEJS_DEVTOOLS)
// } catch (e) {
// console.error('Vue Devtools failed to install:', e.toString())
// }
}
createWindow()
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
})
}
}
-
yarn 安装依赖
-
安装完成后,通过yarn electron:serve 项目就可以运行了
主进程与渲染进程通信需要注意的细节
在低版本的electron上,主进程与渲染进程通信是通过在项目中引入rpcrender模块进行通信的,在高版本上,则需要通过挂载preload插件方式提前注册号相关事件,并在渲染进程通过window上的属性进行调用 文档如下: https://www.electronjs.org/zh/docs/latest/tutorial/ipc
以上
|