首页 > 解决方案 > 如何在 Electron 中保存/加载菜单项的选中状态?

问题描述

我在电子应用程序中有一个菜单,项目看起来像这样:

 const menuTemplate = [
    // ... previous menu items
    {
      label: "Notifications",
      submenu: [
        {
          label: "Enable reset notification",
          type: "checkbox",
          checked: true,
          click: e => {
            mainWindow.showResetNotification = e.checked;
          }
        },
        {
          label: "Reminder notifications",
          submenu: [
            {
              label: "Never",
              type: "radio",
              click: e => {
                if (e.checked) {
                  mainWindow.resetNotification = "never";
                }
              }
            },
            {
              label: "Every 30 minutes",
              type: "radio",
              click: e => { /* ... */ }
            },
            {
              label: "Every hour",
              type: "radio",
              checked: true,
              click: e => { /* ... */ }
            }
          ]
        }
      ]
    }
  ];

我希望能够检查某些内容并保存该状态,以便如果我重新打开应用程序,它会加载选中菜单项的先前状态(选中与未选中)。

我怎样才能在电子中做到这一点?我尝试了本地存储,但因为它是主进程,所以它无权访问它。

提前致谢!

标签: javascriptelectron

解决方案


使用像电子商店这样的包

它的代码少于 100 行,并且只有几个依赖项。

https://github.com/sindresorhus/electron-store


推荐阅读