关于electron打包Web应用打不开,是file协议,无法使用http协议,用express去开启一个http服务就行了。

127 阅读1分钟

具体的可以看我的github:github.com/yedao-zs/el… 

代码实现:

 ``` const { app, BrowserWindow,Menu } = require('electron'); const express = require('express'); const path = require('path'); const createWindow=()=>{ Menu.setApplicationMenu(null) // null值取消顶部菜单栏 // 创建窗口 let mainWindow=new BrowserWindow({ width: 1920, height: 1080, }) // 服务器端口 mainWindow.loadURL('http://localhost:3000'); //调试框 mainWindow.webContents.openDevTools(); // 释放内存 mainWindow.on('closed', function () { mainWindow = null; }); } app.on('ready', () => { const server = express(); // 将静态文件目录设置为打包后的资源路径 server.use(express.static(path.join(__dirname, 'dist'))); // 设置路由,返回 index.html 页面 server.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'dist', 'index.html')); }); // 监听端口 server.listen(3000, () => { console.log('Server running on port 3000'); createWindow(); }); }); app.on('window-all-closed', function () { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', function () { if (mainWindow === null) { createWindow(); } }); ```