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.


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.


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

let iconTary = null
let path = require('path')
let {Tray} = require('electron')
let iconTary = null
app.on('ready',() => {

	iconTary = new Tray(path.join(__dirname,'../static/flag.png'))

problem solved.

Read More: