我已经看到很多人试图从渲染过程中控制日志的问题,这不是我的问题我有console.log乱丢我的主要代码而我在这里看不到我的控制台中的任何内容是我的代码。
/* eslint-disable no-undef */
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
/* eslint-enable */
let win;
console.log('console log test');
function createWindow() {
win = new BrowserWindow({
width: 800,
height: 800
});
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
win.on('close', () => {
win = null;
});
console.log('console log test');
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (win == null) {
console.log('console log test');
createWindow();
console.log('console log test');
}
});

我没有看到除电子本身产生的日志之外的单一日志 我尝试过抛出错误,但是那些工作正常,但任何控制台都没有。*相关根本不起作用,我尝试在PowerShell中运行它并从GitHub重新拉动,我的朋友可以看到控制台当他拉动项目时记录,所以看起来我是孤立的。我还更新了NPM以及与项目相关的所有模块。我尝试创建一个新的控制台并登录到那个但是它似乎没有出现,我错过了什么?我花了好几个小时,准备放弃。
答案 0 :(得分:5)
我感觉到你的痛苦。我的一个盒子(Server2012盒子)上有这个问题。在我遇到其中一个电子问题线程this comment之前,没有什么对我有用。
通常,当你安装电子时,你的package.json中会有一个看起来像这样的脚本。
"scripts": {
"start": "electron .",
}
我改变了我的
"scripts": {
"start": "C:/path/to/project/node_modules/electron-prebuilt/dist/electron.exe .",
}
我开始从powershell中的主电子过程中记录下来。
请注意,如果您使用较新版本的电子,则可能需要将electron-prebuilt
更改为electron
。
答案 1 :(得分:1)
在Windows上,如果要查看包含来自console.log()的所有消息的控制台,则必须使用参数 - enable-logging 启动应用程序,例如:
MyApp.exe --enable-logging
答案 2 :(得分:0)
直接登录浏览器控制台如何? 我发现这对于我的工作流程来说更容易。
所以我要做的是将ipcRenderer上的侦听器设置为主进程,并且每当我需要从主日志中记录事件时,我都将其发送到渲染器上的listerner。
例如,在renderer.js中,我这样设置:
pre.ipcRenderer.on("log", (event,log) => {
console.log(log)
});
在主目录中,无论我需要记录一段代码,只要插入以下代码行即可:
window.webContents.send("log", [__dirname]);
我的假设与上面的代码:
您已在webPreference对象中设置了nodeIntegration: false
。这就是为什么__dirname
之类的代码在渲染器进程中不可用的原因。
由于上述原因,您正在使用预加载器加载所有必需的特定于节点的文件。
我在preload.js文件中定义了所有这些对象,并将它们运送到一个名为window.pre
pre
对象中的预加载模块之一是ipcRenderer。这就是为什么我以pre.ipcRenderer
所有这些都是说,如果您直接从渲染器进程中使用节点,那么您将无需为我的pre.
所困扰,如果不需要,现在您现在就为什么我使用它了
曹。