如何使开发工具默认不显示在屏幕上:电子

Kap*_*pta 4 reactjs electron

我正在使用样板创建一个电子反应应用程序。默认情况下,开发工具会显示在屏幕上。如何使开发工具仅在我要求时才出​​现而在启动时不出现?

另外,控制台中未显示任何错误,因此开发工具也不会出现,因为存在错误。

在此处输入图片说明

Ami*_*uda 17

如果再加上devTools: falsewebPreferences,当你启动应用电子DevTools不会显示。但是,它仍然可以通过按Ctrl+ Shift+来打开I

webPreferences: {
   devTools: false
}
Run Code Online (Sandbox Code Playgroud)

看看 Slack。它是用 Electron 制作的,当您按下Ctrl+ Shift+时,DevTools 不会打开I

我查看了 Electron 的官方文档,我找到了一个解决方案,当您按Ctrl+ Shift+时不允许打开 DevTool I

webPreferences: {
   devTools: false
}
Run Code Online (Sandbox Code Playgroud)

但是,这会阻止所有其他浏览器的 Ctrl + Shift +I 因此,只要您的电子应用程序获得焦点,您就可以编写上述代码。并且,当您的应用模糊时将其删除。通过这种方式,您可以获得针对此问题的适当解决方案。

  • 我无法通过“Ctrl+Shift+I”和“devTools:false”打开开发工具。也许在更新版本的电子中禁用了这一点(我使用的是 v13) (4认同)

小智 8

只需注释或删除main.js文件中的这一行代码(将devTools设置为false)this.mainWindow.openDevTools(); (或)将以下代码添加到

 mainWindow = new BrowserWindow({ 
 width: 1024,
 height: 768,
 webPreferences: {
  devTools: false
  }
Run Code Online (Sandbox Code Playgroud)

}); (或)将package.json构建更改为npm run build && build --win --x64 (或)再次安装npm

  • 如果设置了`this.mainWindow.openDevTools();`,它仍然可以在`View/Toggle Developer Tools`中打开。但如果在`webPreferences`中设置`devTools: false`,`Toggle Developer Tools`将被禁用。 (2认同)

Raf*_*res 7

是什么让应用程序启动时出现的工具开发者是行require('electron-debug')()的src / main.dev.ts。此函数有一个默认为的showDevTools选项true,因此您应该将其更改为:

require('electron-debug')({ showDevTools: false });
Run Code Online (Sandbox Code Playgroud)

您仍然可以使用快捷键CTRL+ ALT+I或按显示开发人员工具F12,如果您想完全禁用它,请设置webPreferences.devToolsfalseon new BrowserWindow

mainWindow = new BrowserWindow({
  // ... other settings
  webPreferences: {
    // ...
    devTools: false,
  },
});
Run Code Online (Sandbox Code Playgroud)


Nar*_*yal 4

只需添加这两行粗体代码即可。打包后你将看不到devTool。

const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
Run Code Online (Sandbox Code Playgroud)

变量调试=假

// 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.
  mainWindow.loadURL(`file://${__dirname}/index.html`)
Run Code Online (Sandbox Code Playgroud)

// 打开开发工具。

if(调试) 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()
  }
})

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
Run Code Online (Sandbox Code Playgroud)