const { app, BrowserWindow } = require('electron'); const path = require('path'); const url = require('url'); // 引入 url 模块处理路径 function createWindow() { const win = new BrowserWindow({ width: 1200, height: 800, webPreferences: { nodeIntegration: true, contextIsolation: false, }, }); const isDev = !app.isPackaged; if (isDev) { win.loadURL('http://localhost:8081'); win.webContents.openDevTools(); } else { // 生产环境:使用 url.format 构建文件路径 // Electron Forge 会将 dist 文件夹打包进 resources/app.asar // 所以路径是 ./dist/index.html win.loadURL( url.format({ pathname: path.join(__dirname, 'dist','index.html'), protocol: 'file:', slashes: true, }) ); } } app.whenReady().then(createWindow);