简体   繁体   English

如何使用electron-devtools-installer?

[英]How to use electron-devtools-installer?

What I have 是)我有的

When it comes to installation and configuration the official documentation states the following: 在安装和配置方面, 官方文档说明如下:

All you have to do now is 你现在要做的就是

 import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer'; installExtension(REACT_DEVELOPER_TOOLS) .then((name) => console.log(`Added Extension: ${name}`)) .catch((err) => console.log('An error occurred: ', err)); 

That is a bit concise. 这有点简洁。

Questions 问题

  • Where should I place this code? 我应该在哪里放置此代码?
  • How should I initialize it? 我应该如何初始化它?
  • How can I add multiple extensions? 如何添加多个扩展程序?

tl;dr TL;博士

Place it in the file where you include Electron and run it after the ready event was emitted by it: 将它放在包含Electron的文件中,并在它发出ready事件后运行它:

const { app } = require('electron');
app.on('ready', functionWithTheCodeFromDocs);

You only need to do this once! 你只需要这样做一次! The extensions will persist after this code have been run. 运行此代码后,扩展将保持不变。

In depth explanation 深入解释

Install the package 安装包

Install the package just like the docs instruct you. 像文档指示您一样安装软件包。 In case of npm: 在npm的情况下:

npm install electron-devtools-installer --save-dev

Require the package 需要包裹

You may require the package and configure it in the file where you build up your Electron app. 您可能需要该软件包并在构建Electron应用程序的文件中对其进行配置。 You need to include the installer function and the (possibly multiple) needed extension(s): 您需要包含安装程序功能和(可能是多个)所需的扩展名:

With ES6 modules: 使用ES6模块:

import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer';

With require : require

const { default: installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = require('electron-devtools-installer');

Configuration and usage 配置和使用

I will presume that somewhere you required Electron: 我会假设你需要Electron:

const { app } = require('electron');

The installExtension function have to be called after the ready event was emitted by the application. 必须在应用程序发出ready事件后调用installExtension函数。 If you want to add multiple extensions you have to call the function multiple times with the different extensions by copy-paste: 如果要添加多个扩展名,则必须通过复制粘贴多次使用不同的扩展名调用该函数:

app.on('ready', () => {
    installExtension(REACT_DEVELOPER_TOOLS)
        .then((name) => console.log(`Added Extension: ${name}`))
        .catch((err) => console.log('An error occurred: ', err));
});
app.on('ready', () => {
    installExtension(REDUX_DEVTOOLS)
        .then((name) => console.log(`Added Extension: ${name}`))
        .catch((err) => console.log('An error occurred: ', err));
});

Or you can write a loop: 或者你可以写一个循环:

 app.on('ready', () => {
    [REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS].forEach(extension => {
      installExtension(extension)
          .then((name) => console.log(`Added Extension: ${name}`))
          .catch((err) => console.log('An error occurred: ', err));
    });
});

If you have done everything properly, after you run electron . 如果你已经完成了所有事情,那么在你运行electron . in your project's folder, you should see this in the console: 在项目的文件夹中,您应该在控制台中看到:

Added Extension: React Developer Tools 添加了扩展:React Developer Tools
Added Extension: Redux DevTools 添加了扩展:Redux DevTools

Keep in mind: You only need to run this code once. 请记住:您只需运行此代码一次。 As Electron's BrowserWindow.addDevToolsExtension documentation states: 正如Electron的BrowserWindow.addDevToolsExtension文档所述:

The extension will be remembered so you only need to call this API once, this API is not for programming use. 扩展名将被记住,因此您只需要调用此API一次,此API不用于编程。 If you try to add an extension that has already been loaded, this method will not return and instead log a warning to the console. 如果您尝试添加已加载的扩展,则此方法不会返回,而是向控制台记录警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM