【问题标题】:Can I create a windows exe from a react project我可以从反应项目创建一个 Windows exe
【发布时间】:2019-12-23 04:00:09
【问题描述】:

我有一个反应项目。我想创建一个可下载的 Windows exe。我可以使用 Zeit pkg 在节点服务器上做到这一点,但我不知道如何通过 react 来做到这一点(或类似的事情)。

我已经尝试过 Zeit pkg。没有看到很多其他选择。

【问题讨论】:

标签: reactjs


【解决方案1】:

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

最难的部分实际上是创建安装程序,我关注this tutorial for windows。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2022-08-13
    • 2021-11-27
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    相关资源
    最近更新 更多