electron热更新
When working on an Electron app, it’s very handy to set up hot reload, so that the application updates without having to restart it.
在Electron应用程序上工作时,设置热重装非常方便,这样应用程序无需重启就可以更新。
You can do that using the npm module electron-reloader.
您可以使用npm模块electronic-reloader做到这一点 。
Suppose you have this sample Electron application:
假设您有以下电子示例应用程序:
index.js
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
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Install electron-reloader
as a development dependency:
安装electron-reloader
作为开发依赖项:
npm install -D electron-reloader
Then add this line to the index.js
file:
然后将此行添加到index.js
文件:
try {
require('electron-reloader')(module)
} catch (_) {}
and that’s it! Now when you start the application using electron .
, or npm start
if you have
就是这样! 现在,当您使用electron .
启动应用程序时electron .
或npm start
如果有)
"start": "electron .",
in your package.json
, any change you apply to the application files will be reflected in the application window.
在package.json
,您对应用程序文件所做的任何更改都会反映在应用程序窗口中。
electron热更新