【问题标题】:My electron app is not starting without any logging我的电子应用程序没有任何日志记录就无法启动
【发布时间】:2019-11-14 18:52:27
【问题描述】:

所以我正在尝试使用 angular(Typescript 版本)制作一个电子应用程序,它根本不会启动,我已经集成了不做任何事情的 Electron-log(这就是我认为它根本不启动的原因)。

const path = require('path');
const url = require('url');
const log = require('electron-log');
log.transports.file.level = 'debug';
log.transports.file.format = '{h}:{i}:{s}:{ms} {text}';
log.transports.file.file = __dirname + './log.txt';
log.transports.file.streamConfig = { flags: 'w'};
log.info('message');
// Keep a global reference of the window object, if we don't the window will be closed
// automagically when the JS object is Garbage Collected. (GC from here on out)

let win;
try {
  const createWindow = () => {
    // set timeout to render the window not until the Angular compiler is ready to show the project
    //create the browser window
    win = new BrowserWindow({
      width: 800,
      height: 600,
      icon: path.join(__dirname, 'favicon.ico'),
    });

    //and load the app
    win.loadURL(url.format({
      pathname: path.join(__dirname, 'resources/app/angular-flask/index.html'),
      protocol: 'file:',
      slashes: true
    }));

    // Emitted when the window is closed
    win.on('closed', () => {
      // Dereference the window object
      win = null;
    });
  };

// This method will be called when Electron has finished initialization and is ready to
// create browser windows . Some API's can only be used after this event happens.
  app.on('ready', createWindow);

// Quit when all windows are closed
  app.on('windows-all-closed', () => {
    // On macOS applications might stay active untill user quits with cmd + Q
    if (process.platform !== 'darwin') {
      app.quit();
    }
  });


  app.on('activate', () => {
    // On macOS sometimes a window is recreated when the dock icon is clicked and there are no other
    // windows open
    if (win === null) {
      createWindow();
    }
  });
} catch(e) {

  log.error(e)
}

以上是我的 Electron Production 代码,也可以在 pastebin 上找到,谁能解释为什么会发生这种情况并提供解决方案?

编辑:在我集成电子日志之前它不起作用。当我通过 npx lite-server 启动它时,角度前端也可以工作。

EDIT2:这是我的 Package.json 和我更新的 main.js 代码 electron.prod.js 我也在使用电子打包器。

【问题讨论】:

  • 在您集成“电子日志”之前它是否有效?
  • 不,在整合 @Kyle 之前我没有工作
  • 我们需要更多。 package.json 至少会有所帮助。在 BrowserWindow 中以 webPreferences: { devTools: true } 开头,在应用程序准备就绪时以 mainWindow.webContents.openDevTools(); 开头。检查控制台是否存在您正在加载的资产 (index.html) 的问题。我知道这很令人沮丧。我刚刚在 Mac 上遇到了类似的问题,它可以工作,但在与 electron-builder 签署后无法加载资产。
  • @narmageddon 我添加了您所说的内容,但是当我单击 exe 时,甚至没有弹出窗口。我还用 package.json 和更新的代码更新了问题
  • @Deathshiver 好的,我可以在resources/app/angular-flask/index.html 找到索引页面。 BrowserWindow 也没有定义。我必须在文件顶部添加 const { app, BrowserWindow } = require("electron");`。我还将 loadUrl 更改为:win.loadURL('file://${path.join(__dirname,"../resources/app/angular-flask/index.html")}');。我从来没有像你那样做过url.format,但我知道我在 Mac 和 Windows 上的工作方式。

标签: javascript angular typescript electron


【解决方案1】:

看起来app 没有定义。

您能否尝试在文件开头添加以下行。

const electron = require('electron');
const app = electron.app;

【讨论】:

  • 刚刚试了一下,不幸的是它也没有工作,我仍然没有得到任何日志记录。
  • 哎呀,让我们再尝试一件事。在“loadUrl”行后添加win.once('ready-to-show', () => { win.show(); })
  • 不,这也行不通。它可能与 win.loadUrl 行中的路径名有关吗?
【解决方案2】:

因此,为了解决我的问题,我回到了tutorial,我关注并检查了那里使用的 package.json,并慢慢地将我拥有的所有内容添加和更新到该 package.json 中。 当它还在工作时,我将它移植到我自己的 package.json 中,然后它就可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多