在 Electron 应用程序中使用 console.log() [英] Using console.log() in Electron app

查看:44
本文介绍了在 Electron 应用程序中使用 console.log()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的 Electron 应用中将数据或消息记录到控制台?

How can I log data or messages to the console in my Electron app?

这个非常基本的 hello world 默认会打开开发工具,因为我无法使用 console.log('hi').Electron 有替代品吗?

This really basic hello world opens the dev tools by default, by I am unable to use console.log('hi'). Is there an alternative for Electron?

ma​​in.js

var app = require('app');
var BrowserWindow = require('browser-window');

require('crash-reporter').start();

var mainWindow = null;

app.on('window-all-closed', function() {
  // Mac OS X - close is done explicitly with Cmd + Q, not just closing windows
  if (process.platform != 'darwin') {
    app.quit();
  }
});

app.on('ready', function(){
  mainWindow = new BrowserWindow({ width: 800, height: 600});

  mainWindow.loadUrl('file://' + __dirname + '/index.html');

  mainWindow.openDevTools();

  mainWindow.on('closed', function(){
    mainWindow = null;
  });
});

推荐答案

console.log 有效,但它记录到的位置取决于您是从主进程还是从渲染器进程调用它.

console.log works, but where it logs to depends on whether you call it from the main process or the renderer process.

如果您从渲染器进程(即 index.html 文件中包含的 JavaScript)调用它,它将被记录到开发工具窗口.

If you call it from the renderer process (i.e. JavaScript that is included from your index.html file) it will be logged to the dev tools window.

如果您从主进程(即在 main.js 中)调用它,它的工作方式与在 Node 中的工作方式相同 - 它将登录到终端窗口.如果您使用 electron . 从终端启动 Electron 进程,您可以从那里的主进程中看到您的 console.log 调用.

If you call it from the main process (i.e. in main.js) it will work the same way as it does in Node - it will log to the terminal window. If you're starting your Electron process from the Terminal using electron . you can see your console.log calls from the main process there.

这篇关于在 Electron 应用程序中使用 console.log()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆