【发布时间】:2021-06-23 18:26:10
【问题描述】:
当我从http://localhost:3000 运行我的 React 构建时,我能够让我的电子构建工作。但是,它仅在我启动 localhost 时才有效。
我对电子很陌生,它甚至可能不是我正在寻找的实用程序。有没有办法让我的电子构建运行而不需要在后面运行本地主机?
我的目标是打包我的 React 应用程序并通过可执行文件在其他设备上运行它,而无需运行服务器或编码软件。
目前我有:
package.json
{
"homepage": "./",
"main": "./public/main.js",
"build": {
"appId": "test",
"compression": "normal",
"asar": true,
"extends": null,
"files": [
"./public/main.js",
"build/**/*",
"node_modules/**/*"
],
"win": {
"target": "portable"
}
}
}
|---public
| |---index.html
| |---main.js
|---src
| |---App.js
| |---index.js
| |...
main.js
const { app, BrowserWindow } = require('electron')
const path = require('path')
const url = require("url")
function createWindow () {
const win = new BrowserWindow({
width: 1600,
height: 1200
})
win.loadURL(
url.format({
pathname: path.resolve(__dirname, "./index.html"),
protocol: "file",
slashes: true
})
)
win.webContents.openDevTools();
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
如果我启动了 localhost 并且 main.js 的路径名转到 "http://localhost:3000" 一切都按预期工作,那么 React 应用程序就会呈现。
当我将路径名指向 path.resolve(__dirname, "./index.html") 时,它会看到 html 并呈现它,但实际上并没有呈现任何 React 应用程序。
【问题讨论】:
标签: reactjs electron electron-builder