Electron 官网地址 点此 : electron 官方地址javascript
Electron 至关于一个浏览器的外壳 , 咱们将 编写的 HTML , CSS , Javascript 网页程序 嵌入进 Electron 里面html
以便于在桌面上进行运行。 通俗来说它就是一个软件 , 如 QQ 、网易云音乐、优酷视频 等等。功能至强大vue
超乎你的想象java
安装 Node.js 坏境node
Node.js 官网地址 点此 node.js 官方地址git
安装 Vue Cligithub
npm install -g @vue/cli
复制代码
安装 淘宝镜像 cnpm ( 因 electron 包 特别大 本人尝试过 npm 或 yarn 的 方法 须要等待很长时间 )web
全局安装 Electronvue-cli
cnpm install electron -g
//若是安装仍是 特比慢 或 不想安装cnpn 能够联系咱们
复制代码
查看 是否安装成功 :npm
1. 首先使用 vue-cli 搭建基础 vue框架
复制代码
和平时写 vue项目同样 选择本身须要的
cd electron-demo 进入项目目录
yarn serve 启动项目
输入 vue add electron-builder 后 按下回车
安装过程当中 此处咱们选择 ^13.0.0 按下回车
因 electron 安装时间过长 致使安装失败
解决办法 直接使用 cnpm install 进行安装
安装 完成后 咱们使用 npm run electron:serve 运行项目 依旧是报错的。请耐心往下看完
此处 咱们须要打开项目的目录 找到 node_modules 下的 electron 。 删除 这个文件之后 从新使用 cnpm install
至此。 electron 应用 已经搭建完成。 咱们使用 npm run electron:serve 进行启动项目 ( windows 与 imac 同样 )
已经完美运行成功 能够看到桌面已经弹出 electron-demo 应用
相比于 基础 vue 项目 咱们能够发如今目录中 多出一个 background.js 文件
此处能够 配置 electron 应用的一些设置。 如窗口大小 、 是否能够缩放、是否能够移动窗口等等
Background.js 文件
'use strict'
import { app, protocol, BrowserWindow } 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.
const win = new BrowserWindow({
width: 1120,
height: 1090,
minHeight: 700,
minWidth: 1000,
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 win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
// if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
createProtocol('app')
// Load the index.html when not in development
win.loadURL('app://./index.html')
}
}
// 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()
})
}
}
复制代码
注意:打开和 关闭 开发者工具 请点击。 操做栏 > view > Toggle Developer tools
咱们能够在官网的文档里搜索一下。进行一些配置 ( 详细配置请查看官网配置 ) 连接已经放在文章开头啦
打包应用 使用 npm run electron:build 进行打包 ( imac 系统 和 windows系统 同样使用这个 )
此处 以 iMac 为例 ( 下面会有 windows 解决方案 )
当咱们看到。 Build complete ! 时 就说明 打包成功 能够查看 dist_electron / mac 里面就是打包好的应用 ( 苹果系统 )