×

Vue应用:使用electron转换项目成桌面应用

作者:abc1232022.12.14来源:Web前端之家浏览:2538评论:0
关键词:vuejs

Vue应用:使用electron转换项目成桌面应用。

1、将已有 Vue 项目打包。

2、新建 main.js、package.json 文件

将打包生成的 index.html、js、css、然后再和新建的 main.js、package.json 文件 放至一个目录下。并命令行切换至这个目录。

新建的 main.js 如下:

const { app, BrowserWindow } = require("electron"); // 引入electron
let win;
let windowConfig = {
  width: 800,
  height: 600,
}; // 窗口的大小
function createWindow() {
  win = new BrowserWindow(windowConfig); // 创建一个窗口
  win.loadURL(`file://${__dirname}/index.html`); // 加载打包生成的index.html
  win.webContents.openDevTools(); // 开启调试工具
  win.on("close", () => {
    //回收 BrowserWindow 对象
    win = null;
  });
  win.on("resize", () => {
    win.reload();
  });
}
app.on("ready", createWindow);
app.on("window-all-closed", () => {
  app.quit();
});
app.on("activate", () => {
  if (win == null) {
    createWindow();
  }
});

新建的 package.json 如下:

{
    "name": "demo",
    "productName": "项目名称",
    "author": "作者",
    "version": "1.0.4",
    "main": "main.js",
    "description": "项目描述",
    "scripts": {
        "pack": "electron-builder --dir",
        "dist": "electron-builder",
        "postinstall": "electron-builder install-app-deps"
    },
    "build": {
        "electronVersion": "1.8.4",
        "win": {
            "requestedExecutionLevel": "highestAvailable",
            "target": [
                {
                    "target": "nsis",
                    "arch": [
                        "x64"
                    ]
                }
            ]
        },
        "appId": "demo",
        "artifactName": "demo-${version}-${arch}.${ext}",
        "nsis": {
            "artifactName": "demo-${version}-${arch}.${ext}"
        },
        "extraResources": [
            {
                "from": "./static/xxxx/",
                "to": "app-server",
                "filter": [
                    "**/*"
                ]
            }
        ]
    },
    "dependencies": {
        "core-js": "^2.4.1",
        "electron-packager": "^12.1.0",
        "electron-updater": "^2.22.1"
    },
    "devDependencies": {
        "electron-forge": "^5.2.4"
    }
}

3、安装包:

yarn install

(报错不用管,能 electron . 运行成功且效果正常就行)

4、运行:

electron .

5、注意事项:

  • vue 项目 路由用 hash 模式。

  • vue 项目 的 vue.config.js 要配置 publicPath: './'

(因为若不配置,则 electron 文件路径不对)

module.exports = {
   lintOnSave: false,
   publicPath: './',
      css: ....
      ....
}

index.html 中文件路径如以下正确显示:

<link rel="icon" href="favicon.ico" rel="external nofollow" >
<title>efficiency-helper</title>
<link href="css/chunk-11991773.33db9af5.css" rel="external nofollow"  rel="prefetch">
<link href="css/chunk-17ca335a.ad6ca46b.css" rel="external nofollow"  rel="prefetch">
<link href="css/chunk-3dde8fae.019eaf8d.css" rel="external nofollow"  rel="prefetch">
<link href="css/chunk-4c9aec9b.410cb728.css" rel="external nofollow"  rel="prefetch">
<link href="css/chunk-f52405ee.f4abe7d9.css" rel="external nofollow"  rel="prefetch">

若不配置 publicPath: './' 则:href=“/css/chunk-17ca335a.ad6ca46b.css” 路径错误。导致应用出现白屏。

您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/vuejs20221214a1.html

网友评论文明上网理性发言 已有0人参与

发表评论: