如何在Electron上设置热重装

在Electron应用程序上工作时,设置热重装非常方便,这样应用程序无需重启就可以更新。

您可以使用npm模块做到这一点电子再装弹机

假设您有以下电子示例应用程序:

index.js

const { app, BrowserWindow } = require('electron')

function createWindow() { // Create the browser window. const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, }, })

// and load the index.html of the app. win.loadFile(‘index.html’) }

app.whenReady().then(createWindow)

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

安装electron-reloader作为开发依赖项:

npm install -D electron-reloader

Then add this line to the index.js file:

try {
  require('electron-reloader')(module)
} catch (_) {}

and that’s it! Now when you start the application using electron ., or npm start if you have

"start": "electron .",

in your package.json, any change you apply to the application files will be reflected in the application window.


More devtools tutorials: