gpt4 book ai didi

reactjs - 我可以从 React 项目创建 Windows exe 吗

转载 作者:行者123 更新时间:2023-12-05 01:39:54 26 4
gpt4 key购买 nike

我有一个 React 项目。我想创建一个可下载的 Windows exe。我可以使用 Zeit pkg 使用节点服务器来完成此操作,但我不知道如何使用 React 来做到这一点(或类似的事情)。

我试过 Zeit pkg。没有看到很多其他选项。

最佳答案

电子是要走的路,像 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

最难的部分实际上是创建安装程序,我遵循了this tutorial window 。

关于电子的更多信息here .希望对您有所帮助。

关于reactjs - 我可以从 React 项目创建 Windows exe 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57531216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com