h2> 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
h2>
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 h2>
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) code> internal part to use
code: p>
//创建系统托盘图标
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.