Heim > Web-Frontend > js-Tutorial > Hauptteil

ElectronJs 中的热重载

王林
Freigeben: 2023-08-30 12:17:09
nach vorne
1356 人浏览过

ElectronJs 中的热重载

热重载是 ElectronJS 中的一项强大功能,它可以让开发人员快速实时查看代码更改,而无需重新启动应用程序。通过减少测试变更所需的时间和精力,它使开发过程更快、更高效。

在 ElectronJS 中实现热重载的步骤

热重载功能是使用一个名为“Electron-Reload”的库实现的,通过几个简单的步骤就可以轻松地将其集成到Electron JS应用程序中。用户可以按照以下步骤在 Electron Js 中实现热重载 -

安装电子重载模块

在 Electron JS 中实现热重载的第一步是安装 electro-reload 模块。用户可以使用 npm 安装它,如下所示 -

npm install electron-reload 
Nach dem Login kopieren

在主进程中需要电子重新加载

一旦安装了电子重载模块,我们就需要在电子应用程序的主进程中使用它。我们可以通过将以下代码添加到 main.js 文件中来实现 -

const electronReload = require('electron-reload');
electronReload(__dirname);
Nach dem Login kopieren

重新加载渲染器进程

最后一步是每当我们的代码发生更改时重新加载渲染器进程。我们可以通过将以下代码添加到 renderer.js 文件来做到这一点 -

if (module.hot) {
   module.hot.accept();
}
Nach dem Login kopieren

热重载的高级使用和自定义选项

Electron 中的热重新加载很容易设置,但“电子重新加载”模块提供了额外的选项,例如忽略特定文件和文件夹以及从重新加载中排除某些模块。

ElectronJS 中实现热重载的重要函数

在热重载中,需要了解一些重要的函数,以便在我们的 Electron 应用程序中实现它 -

module.hot.accept() - 此函数在渲染器进程中使用,以启用渲染器进程的热重载。当渲染器进程代码发生更改时,渲染器进程将自动重新加载,并且更改将实时反映在应用程序中。

ElectronReload(__dirname) - 此函数在主进程中使用,以启用主进程的热重载。每当代码发生更改时,它都会重新加载主进程,使我们能够实时查看更改的效果。

app.on('ready', () => {...}) - 当 Electron 应用程序准备好向用户显示时,将调用此事件处理函数。它通常用于创建主窗口并加载初始 HTML 文件。

BrowserWindow - 此类用于在 Electron 应用程序中创建新窗口。在主进程中,我们可以创建 BrowserWindow 的实例并设置各种选项,例如大小和 Web 首选项,以自定义每个窗口的外观和行为。

这些函数是理解 Electron JS 中热重载如何工作的关键,并且在本教程前面提供的示例中使用。通过了解如何使用这些函数,我们可以在 Electron 应用程序中实现热重载,并实时更改主进程和渲染器进程。

示例

在此示例中,首先我们使用 electro.app 模块创建一个新的 ElectronJS 应用程序,并使用 on 方法注册一个回调函数,该函数在应用程序准备就绪时触发。在此回调函数中,我们使用 Electron.BrowserWindow 模块创建一个新的浏览器窗口,并将 index.html 文件加载到其中。

接下来,在 renderer.js 文件中,我们使用 module.hot 属性在渲染器进程中启用热重载。这样,如果我们对 renderer.js 文件进行任何更改,更新的代码将自动重新加载。

最后,我们使用 console.log 来记录消息“Hello World!”到控制台。

index.html 文件是一个简单的 HTML 文件,它显示标题和段落并用作应用程序的 UI。

main.js

// main.js 
const { app, BrowserWindow } = require('electron');
const electronReload = require('electron-reload');
electronReload(__dirname);
let win;
app.on('ready', () => {
   win = new BrowserWindow({
      width: 800,
      height: 600,
      webPreferences: {
         nodeIntegration: true
      }
   });
   win.loadFile('index.html');
}); 
Nach dem Login kopieren

渲染器.js

// renderer.js
if (module.hot) {
   module.hot.accept();
}
document.getElementById('root').innerHTML = 'Hello, Hot Reloading!'; 
Nach dem Login kopieren

index.html



    Electorn Js 

Hot Reload in ElectornJs

With hot reloading enabled, any changes made to the code in the "main.js" or "renderer.js" files will be reflected in the application in real-time without requiring a full restart of the application.

Nach dem Login kopieren

示例

在此示例中,ElectronJs 应用程序设置为在触发“ready”事件时创建一个新窗口,并且该窗口加载 index.html 文件。

“renderer.js”文件包含热模块重新加载(HMR)语句,每当代码发生更改时,该语句都会重新加载渲染器进程。它还记录“Hello World!”到控制台。

“index.html”文件显示一个标题和一个段落,表明热重载已启用。

main.js

// main.js
const electron = require('electron');
const electronReload = require('electron-reload');
electronReload(__dirname);
const app = electron.app;
app.on('ready', createWindow);

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

   // and load the index.html of the app.
   win.loadFile('index.html')
} 
Nach dem Login kopieren

渲染器.js

// renderer.js
if (module.hot) {
   module.hot.accept();
}
console.log('Hello World!');
Nach dem Login kopieren

index.html



    Electorn Js 

Hot Reload Enabled

Hot reloading allows for a faster development experience as we can see the effects of our changes immediately.

Nach dem Login kopieren

在本教程中,用户了解了 ElectronJS 中的热重载功能,以及它如何通过允许开发人员实时查看代码更改的效果而无需重新启动整个应用程序来使开发过程更快、更高效。

用户还了解了 ElectronJS 中实现热重载的关键功能和步骤,例如在主进程中要求 Electron-reload 模块、重载渲染器进程以及理解 module.hot.accept( 等函数) )、electronReload(__dirname)、app.on('ready', () => {...}) 和 BrowserWindow。通过遵循这些步骤并理解这些关键功能,用户可以在我们的 ElectronJS 应用程序中实现热重载,并实时更改主进程和渲染器进程。

以上是ElectronJs 中的热重载的详细内容。更多信息请关注PHP中文网其他相关文章!

Quelle:tutorialspoint.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!