广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue使用electron转换项目成桌面应用方法介绍
  • 934
分享到

Vue使用electron转换项目成桌面应用方法介绍

Vue使用electron转换桌面应用Vue electron 2022-11-13 19:11:18 934人浏览 薄情痞子
摘要

1、将已有 Vue 项目打包。 2、新建 main.js、package.JSON 文件 将打包生成的 index.html、js、CSS、然后再和新建的 main.js、packa

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” 路径错误。导致应用出现白屏。

到此这篇关于Vue使用electron转换项目成桌面应用方法介绍的文章就介绍到这了,更多相关Vue electron内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Vue使用electron转换项目成桌面应用方法介绍

本文链接: https://www.lsjlt.com/news/170704.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作