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

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

问题描述

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

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

这个非常基本的hello世界默认情况下会打开开发工具,因为我无法使用 console.log('hi')

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?

main.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天全站免登陆