即使我将托盘的声明放在全局变量中,电子托盘也会消失

Electron tray disappearing even i putting the declaration of the tray in a global variable

我用 electron forge 创建了一个项目(使用 webpack、react 和 typescript 设置),我做的第一件事就是尝试在应用程序中添加一个托盘。但是当应用程序初始化时,大约 3 秒后,托盘消失了。我读了一些关于这个错误的帖子,每个人都说把托盘放在一个全局变量中是解决方案,但对我来说这不起作用,托盘仍然消失

import { app, BrowserWindow, Menu, Tray } from 'electron';

declare const MAIN_WINDOW_WEBPACK_ENTRY: string;

let icon = null

if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

const createWindow = (): void => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    height: 600,
    width: 800,
  });

 
  mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);

  
  mainWindow.webContents.openDevTools();
  icon = new Tray('')

  const contextMenu = Menu.buildFromTemplate([
    {
      label: 'teste', click: () => console.log('hello world')
    }
  ])
  icon.setContextMenu(contextMenu)
};


app.on('ready', createWindow);


app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // 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 (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

错误在行

icon = new Tray('')

传递无效的 url 并没有像我想的那样生成空白图标,它使应用程序崩溃并且托盘消失,所以我需要放置一个有效的图标 url