问题描述
electron
打包后的软件在设置开机自启动后,正常启动托盘图标可以显示,但开启自启动后,托盘图标却无法显示
问题解决
tray = new Tray(path.join(__dirname, './public/logo.png')); //必须是绝对路径和扩展名,像.png等
我的问题是图标之前设置为相对路径,而导致无法显示。将Tray
的图标路径设定为绝对路径后,问题解决。
扩展
自启动的两种方式:
- 通过自带
API
app.setLoginItemSettings({
openAtLogin: true,
path: app.getPath('exe'),
});
- 通过插件
jsoncelectron-auto-launch
const AutoLaunch = require('electron-auto-launch');
let autoLaunch = new AutoLaunch({
name: '可执行文件名称',
path: app.getPath('exe'),
});
autoLaunch.isEnabled().then((isEnabled) => {
if (!isEnabled) autoLaunch.enable();
});