亲宝软件园·资讯

展开

AntDesignPro使用electron构建桌面应用示例详解

不想赖床 人气:0

注意事项声明

主要分为两个部分

开发环境使用

安装 electron 包

npm install electron --save-dev

package.js 添加入口文件

  "main": "main.js",

创建 main.js 入口文件 内容如下

const {app, BrowserWindow, dialog} = require('electron'); 
const path = require('path');
const url = require('url');
​
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
​
function createWindow () {
​
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 1300,
    height: 800,
    frame: true,
    autoHideMenuBar: true,
    fullscreenable: true,
    transparent: false,
    webPreferences: {
      javascript: true,
      plugins: true,
      nodeIntegration: true, // Nodejs
      webSecurity: false,
      preload: path.join(__dirname, './preload.js')
    }
  });
  // mainWindow.webContents.openDevTools();//打开调试工具
​
    //测试时使用mainWindow.loadURL(http://localhost:80000);
    //打包时加载本地文件
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'dist/index.html'),
    protocol: 'file:',
    slashes: true
  }));
​
  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}
​
// 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', createWindow)
​
// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // 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', 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 (mainWindow === null) createWindow()
});

preload.js 文件内添加, 将 electron 做全局导入 未做此操作无法在其他地方使用 electron 模块

global.electron = require('electron')

在 package.json 文件中加入启动命令

  "scripts": {
    "electron-start": "electron .",
  },

试启动 electron 窗口内容加载成功则成功

npm run electron-start

渲染进程如需和主进程通信查看官方文档

https://electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes

打包应用配置

config/config.js 文件添加

history: 'hash', //更改路由方式
publicPath: './', //使打包后的文件使用相对路径

src/utils/request.js 此目录并非标准 不同版本下文件可能有所区别 重点在于给请求配置前缀

当项目打包成应用后使用的是 file:协议 ant pro 的请求无法发出 需要使用完整的请求地址 目前方法为配置前缀

/**
* 配置request请求时的默认参数
*/
const request = extend({
  errorHandler, // 默认错误处理
  prefix: 'http://hotel-system.yc384.com/api', // 请求前缀
  credentials: 'include', // 默认请求是否带上cookie
});

package.json配置打包后的路径方式

"homepage": ".",

使用 electron-builder 打包 exe 文件或者安装包,压缩包

安装

npm install electron-builder

package.json添加命令 (打包windows)

"electron-build": "electron-builder --win --x64"

添加打包配置

"build": {
"appId": "com.xxx.app",
"directories": {
  "output": "build"
}, // 打包后存放目录
  "mac": {// mac安装包dmg
  "target": ["dmg","zip"]
},
"win": {// win安装包nsis
  "target": ["nsis","zip"]
}

创建app目录(builder默认打包app下内容,否则会打包当前所有内容)

将ant pro打包后的dist文件和main.js放入app目录

在app下创建package.json文件(外层package做打包使用,app下的package是打包后的应用依赖)

"name": "hotel",
"version": "2.3.1",
"main": "main.js",

执行打包命令

打包后文件会在 build 目录下

npm run electron-build

使用 electron-packager 打包成 exe 文件

安装electron-package

npm install electron-packager --save-dev

package.json下script添加命令(具体含义百度)

"electron-package": "electron-packager . hotelSystem --win32 --out app --arch=x64 --overwrite --ignore=node_modulesls --electron-version=6.0.5",

执行命令

npm run electron-package

提示

"name": "hotel",
"version": "2.3.1",
"main": "main.js",
"homepage": ".",
"scripts": {
  "electron-start": "electron .",
}

加载全部内容

相关教程
猜你喜欢
用户评论