我有一个反应项目。 我想创建一个可下载的 Windows exe。 我可以使用 Zeit pkg 使用节点服务器来完成此操作,但我不知道如何使用 React 来完成此操作(或类似的操作)。
我尝试过 Zeit pkg。 没有看到很多其他选择。
Electron 是正确的选择,像 cordova 一样使用起来非常简单,这是一个 hello world 示例:
安装
npm install -g electron
创建一个 main.js 文件并添加以下内容:
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
// 'public' is the path where webpack bundles my app
mainWindow.loadURL(`file://${__dirname}/public/index.html`);
// Open the DevTools.
mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
最后运行应用程序:
electron main.js
最难的部分实际上是创建安装程序,我遵循了适用于 Windows 的本教程。
有关电子的更多信息这里。希望有帮助。
先生,我的项目完全在Chrome中运行,但没有在电子中生成exe,错误堆内存错误在此处输入图像描述rs