Skip to main content

MenuItem

类:MenuItem

🌐 Class: MenuItem

将项目添加到本地应用菜单和上下文菜单。

进程:主进程

🌐 Process: Main

请参阅 Menu 了解示例。

🌐 See Menu for examples.

warning

Electron 内置的类不能在用户代码中被继承。 欲了解更多信息,请参见 常见问题

new MenuItem(options)

  • options 对象
    • click 函数(可选)——在菜单项被点击时,会传入 click(menuItem, window, event) 调用该函数。
    • role 字符串(可选)- 可以是 undoredocutcopypastepasteAndMatchStyledeleteselectAllreloadforceReloadtoggleDevToolsresetZoomzoomInzoomOuttoggleSpellCheckertogglefullscreenwindowminimizeclosehelpaboutserviceshidehideOthersunhidequitshowSubstitutionstoggleSmartQuotestoggleSmartDashestoggleTextReplacementstartSpeakingstopSpeakingzoomfrontappMenufileMenueditMenuviewMenushareMenurecentDocumentstoggleTabBarselectNextTabselectPreviousTabshowAllTabsmergeAllWindowsclearRecentDocumentsmoveTabToNewWindowwindowMenu - 定义菜单项的操作,当指定时,click 属性将被忽略。请参见 roles
    • type 字符串(可选)
      • normal
      • separator
      • submenu
      • checkbox
      • radio
      • header - 仅适用于 macOS 14 及更高版本。
      • palette - 仅适用于 macOS 14 及更高版本。
    • label 字符串(可选)
    • sublabel 字符串(可选)macOS - 在 macOS 14.4 及更高版本可用
    • toolTip 字符串(可选)macOS - 此菜单项的悬停文本。
    • accelerator 字符串(可选)- 一个 Accelerator 字符串。
    • icon (NativeImage | 字符串)(可选)- 可以是一个 NativeImage 或图标的文件路径。
    • enabled 布尔值(可选)- 如果为 false,菜单项将显示为灰色并且无法点击。
    • acceleratorWorksWhenHidden 布尔值(可选) macOS - 默认为 true,当为 false 时,如果该项目不可见,将阻止加速键触发该项目。
    • visible 布尔值(可选)- 如果为 false,该菜单项将被完全隐藏。
    • checked 布尔值(可选)- 仅应为 checkboxradio 类型的菜单项指定。
    • registerAccelerator 布尔值(可选) Linux Windows - 如果为 false,加速键将不会注册到系统,但仍会显示。默认值为 true。
    • sharingItem SharingItem(可选)macOS - 当 roleshareMenu 时要共享的项目。
    • submenu(MenuItemConstructorOptions[] | Menu)(可选)- 应为 submenu 类型的菜单项指定。如果指定了 submenu,则可以省略 type: 'submenu'。如果该值不是 Menu,则会使用 Menu.buildFromTemplate 自动将其转换为 Menu
    • id 字符串(可选)- 在单个菜单中唯一。如果定义,则可以通过 position 属性将其用作对该项目的引用。
    • before string[](可选)- 将此项插入到具有指定 ID 的项之前。如果引用的项不存在,该项将插入到菜单的末尾。同时,这也意味着相关的菜单项应与该项放置在同一“组”中。
    • after string[](可选)- 在具有指定 ID 的项目之后插入此项目。如果所引用的项目不存在,该项目将插入到菜单的末尾。
    • beforeGroupContaining string[](可选)- 提供了一种方法,使单个上下文菜单可以声明其所包含的组在具有指定 ID 的项目的包含组之前的位置。
    • afterGroupContaining string[](可选)- 提供一种方法,使单个上下文菜单可以声明其所包含组的位置,该位置位于具有指定 ID 的项的所属组之后。
note

acceleratorWorksWhenHidden 被指定为仅适用于 macOS,因为在 Windows 和 Linux 上,当菜单项被隐藏时,快捷键始终有效。向用户提供此选项,是为了让他们可以关闭它,因为在原生 macOS 开发中这是可行的。

实例属性

🌐 Instance Properties

MenuItem 实例上可用的属性如下:

🌐 The following properties are available on instances of MenuItem:

string 表示该物品的唯一标识符。此属性可以动态更改。

🌐 A string indicating the item's unique id. This property can be dynamically changed.

string 表示物品的可见标签。

🌐 A string indicating the item's visible label.

当菜单项接收到点击事件时触发的 Function。 它可以通过 menuItem.click(event, focusedWindow, focusedWebContents) 调用。

🌐 A Function that is fired when the MenuItem receives a click event. It can be called with menuItem.click(event, focusedWindow, focusedWebContents).

一个 Menu(可选),包含菜单项的子菜单(如果有的话)。

🌐 A Menu (optional) containing the menu item's submenu, if present.

string 表示项目的类型。可以是 normalseparatorsubmenucheckboxradioheaderpalette

🌐 A string indicating the type of the item. Can be normal, separator, submenu, checkbox, radio, header or palette.

note

headerpalette 仅在 macOS 14 及以上版本可用。

一个可选的 string,表示该项目的角色(如果已设置)。可以是 undoredocutcopypastepasteAndMatchStyledeleteselectAllreloadforceReloadtoggleDevToolsresetZoomzoomInzoomOuttoggleSpellCheckertogglefullscreenwindowminimizeclosehelpaboutserviceshidehideOthersunhidequitstartSpeakingstopSpeakingzoomfrontappMenufileMenueditMenuviewMenushareMenurecentDocumentstoggleTabBarselectNextTabselectPreviousTabshowAllTabsmergeAllWindowsclearRecentDocumentsmoveTabToNewWindowwindowMenu

🌐 A string (optional) indicating the item's role, if set. Can be undo, redo, cut, copy, paste, pasteAndMatchStyle, delete, selectAll, reload, forceReload, toggleDevTools, resetZoom, zoomIn, zoomOut, toggleSpellChecker, togglefullscreen, window, minimize, close, help, about, services, hide, hideOthers, unhide, quit, startSpeaking, stopSpeaking, zoom, front, appMenu, fileMenu, editMenu, viewMenu, shareMenu, recentDocuments, toggleTabBar, selectNextTab, selectPreviousTab, showAllTabs, mergeAllWindows, clearRecentDocuments, moveTabToNewWindow or windowMenu

如果设置了,Accelerator | null 表示该物品的快捷键。

🌐 An Accelerator | null indicating the item's accelerator, if set.

🌐 menuItem.userAccelerator Readonly macOS

一个 Accelerator | null,表示该菜单项的 用户分配快捷键

🌐 An Accelerator | null indicating the item's user-assigned accelerator for the menu item.

note

该属性仅在将 MenuItem 添加到 Menu 后才会被初始化。可以通过 Menu.buildFromTemplateMenu.append()/insert() 添加。在初始化之前访问,只会返回 null

NativeImage | string(可选)表示该物品的图标,如果已设置的话。

🌐 A NativeImage | string (optional) indicating the item's icon, if set.

string 表示该物品的子标签。

🌐 A string indicating the item's sublabel.

一个 string,表示物品的悬停文本。

🌐 A string indicating the item's hover text.

boolean 表示该项是否已启用。此属性可以动态更改。

🌐 A boolean indicating whether the item is enabled. This property can be dynamically changed.

boolean 表示该项是否可见。此属性可以动态更改。

🌐 A boolean indicating whether the item is visible. This property can be dynamically changed.

boolean 表示该项是否被选中。此属性可以动态更改。

🌐 A boolean indicating whether the item is checked. This property can be dynamically changed.

当选择 checkbox 菜单项时,将切换 checked 属性的开关状态。

🌐 A checkbox menu item will toggle the checked property on and off when selected.

点击 radio 菜单项时,它的 checked 属性将会被开启,并且同一菜单中所有相邻的菜单项的该属性将被关闭。

🌐 A radio menu item will turn on its checked property when clicked, and will turn off that property for all adjacent items in the same menu.

你可以添加一个 click 函数来实现额外的功能。

🌐 You can add a click function for additional behavior.

boolean 表示加速器是否应注册到系统中或仅显示出来。

🌐 A boolean indicating if the accelerator should be registered with the system or just displayed.

该属性可以动态更改。

🌐 This property can be dynamically changed.

roleshareMenu 时,SharingItem 表示要共享的项目。

🌐 A SharingItem indicating the item to share when the role is shareMenu.

该属性可以动态更改。

🌐 This property can be dynamically changed.

number 表示物品的顺序唯一标识符。

🌐 A number indicating an item's sequential unique id.

物品所属的 Menu

🌐 A Menu that the item is a part of.