答案 0 :(得分:6)
使开发者工具在应用程序启动时出现的是 src/main.dev.ts 中的 require('electron-debug')()
行。此函数有一个 showDevTools
option,默认为 true
,因此您应该将其更改为:
require('electron-debug')({ showDevTools: false });
您仍然可以使用快捷键 CTRL + ALT + I 或按 F12,如果您想完全禁用它,请在 webPreferences.devTools
上将 false
设置为 new BrowserWindow
:
mainWindow = new BrowserWindow({
// ... other settings
webPreferences: {
// ...
devTools: false,
},
});
答案 1 :(得分:4)
在电子中,如果我们将devtools编写为false,则在加载电子应用程序时不会显示。但是,仍然可以通过ctrl + shift + I打开它。
您可以看看Slack之类的应用程序,它是一个电子应用程序。但是,仍然不能打开devtools。
通过阅读electronic js的官方文档,我找到了一个解决方案,即使尝试使用devtools快捷方式ctrl+shift+I
,也可以使devtool打开。
const { app, globalShortcut } = require('electron')
app.on('ready', () => {
// Register a 'Control+Shift+I' shortcut listener.
globalShortcut.register('Control+Shift+I', () => {
//this will get call for Control+Shift+I.
and just return nothing or return false
})
})
因此,请尝试执行此操作,这肯定会禁用devtool快捷方式。
答案 2 :(得分:3)
如果您删除此行,我认为您可以在不启用devtools的情况下运行:https://github.com/chentsulin/electron-react-boilerplate/blob/master/main.development.js#L56
答案 3 :(得分:2)
只需注释或删除main.js文件中的这一行代码(将devTools设置为false)this.mainWindow.openDevTools();
(要么)
将以下代码添加到
mainWindow = new BrowserWindow({
width: 1024,
height: 768,
webPreferences: {
devTools: false
}
});
(要么)
将package.json构建更改为npm run build && build --win --x64
(要么)
再次安装npm
答案 4 :(得分:1)
只需添加这两行粗体代码即可。包装后你不会看到devTool。
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
var debug = false
// 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`)
//打开DevTools。
if(debug)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.
答案 5 :(得分:0)
只想补充一点,如果您想仅在生产模式下禁用 devTools,您可以这样做:
new BrowserWindow({
webPreferences: {
devTools: !app.isPackaged,
},
})
附言这也可以防止使用像 Ctrl+Shift+I 这样的快捷方式来切换 devTools。