Solve the problem that the tray of electron system disappears automatically

on pit

turn on electron, and found that the system tray icon would automatically disappear after a period of time. To solve this problem, I found the reason why the system tray icon would automatically disappear.

Reason

when we declare a variable in Tray, we do not declare it as a global variable, which results in it being recycled by the garbage collection mechanism during operation.

solution

declares the variable of the storage window/system tray as a global variable, that is, it is declared before app.on('ready',fuc) in main.js. In the app. On (' ready 'fuc) internal part to use
code:

//创建系统托盘图标 let iconTary = null let path = require('path') let {Tray} = require('electron') //将iconTary声明为全局变量 let iconTary = null app.on('ready',() => { //将iconTary赋值Tary模块 iconTary = new Tray(path.join(__dirname,'../static/flag.png')) }) 

problem solved.

Read More: