将打包生成的 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"
}
}
yarn install
(报错不用管,能 electron .
运行成功且效果正常就行)
electron .
hash
模式。publicPath: './'
module.exports = {
lintOnSave: false,
publicPath: './',
css: ....
....
}
<link rel="icon" href="favicon.ico">
<title>efficiency-helpertitle>
<link href="css/chunk-11991773.33db9af5.css" rel="prefetch">
<link href="css/chunk-17ca335a.ad6ca46b.css" rel="prefetch">
<link href="css/chunk-3dde8fae.019eaf8d.css" rel="prefetch">
<link href="css/chunk-4c9aec9b.410cb728.css" rel="prefetch">
<link href="css/chunk-f52405ee.f4abe7d9.css" rel="prefetch">
publicPath: './'
则:href=“/css/chunk-17ca335a.ad6ca46b.css” 路径错误。导致应用出现白屏。