const {
app, BrowserWindow } = require('electron') function createWindow () {
// 创建浏览器窗口 const win = new BrowserWindow({
width: 800, height: 600, webPreferences: {
nodeIntegration: true } }) // 并且为你的应用加载index.html win.loadFile('index.html') // 打开开发者工具 win.webContents.openDevTools() } // Electron会在初始化完成并且准备好创建浏览器窗口时调用这个方法 // 部分 API 在 ready 事件触发后才能使用。 app.whenReady().then(createWindow) //当所有窗口都被关闭后退出 app.on('window-all-closed', () => {
// 在 macOS 上,除非用户用 Cmd + Q 确定地退出, // 否则绝大部分应用及其菜单栏会保持激活。 if (process.platform !== 'darwin') {
app.quit() } }) app.on('activate', () => {
// 在macOS上,当单击dock图标并且没有其他窗口打开时, // 通常在应用程序中重新创建一个窗口。 if (BrowserWindow.getAllWindows().length === 0) {
createWindow() } }) // 您可以把应用程序其他的流程写在在此文件中 // 代码 也可以拆分成几个文件,然后用 require 导入。
5、根目录下新建index.html文件
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1>Hello Electron!!!</h1> </body> </html>
6、在package.json文件中配置electron命令
"scripts": {
"start": "electron .", }
7、命令窗口执行 npm run start 命令可以调试内容
到目前为止,生成了一个非常简单的demo。
作为一个桌面应用程序,我们希望直接点击exe文件进行运行。因此用到了electron-packager插件。
"scripts": {
"start": "electron .", "pack": "electron-packager . myFirstElectron --win --out ./dist --arch=x64 --app-version=0.0.1 --electron-version=4.1.4" }
打包命令代码的含义解释:
“.”:需要打包的应用目录(.代表当前目录)。
“myFirstElectron”:应用名称。
“–win”:打包平台(windows平台)
” –out ./dist”“:输出目录
“–arch=x64”:64位
“–app-version=0.0.1”:应用版本
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/207766.html原文链接:https://javaforall.net
