跳转到主内容

MenuItem

菜单项

添加菜单项到应用程序菜单和上下文菜单中

Process: Main

See Menu for examples.

[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.

new MenuItem(options)

  • 选项 对象
    • click 函数(可选) - 单击菜单项时,将用 click(menuItem, window, event) 调用。
      • menuItem MenuItem
      • window BaseWindow | undefined - This will not be defined if no window is open.
      • event KeyboardEvent
    • role string (optional) - 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, showSubstitutions, toggleSmartQuotes, toggleSmartDashes, toggleTextReplacement, startSpeaking, stopSpeaking, zoom, front, appMenu, fileMenu, editMenu, viewMenu, shareMenu, recentDocuments, toggleTabBar, selectNextTab, selectPreviousTab, showAllTabs, mergeAllWindows, clearRecentDocuments, moveTabToNewWindow or windowMenu - Define the action of the menu item, when specified the click property will be ignored. See roles.
    • type string(可选)
      • 普通
      • separator
      • submenu
      • 复选框
      • radio
      • header - Only available on macOS 14 and up.
      • palette - Only available on macOS 14 and up.
    • label string (可选)
    • sublabel string (optional) macOS - Available in macOS >= 14.4
    • toolTip string (可选) macOS - 菜单项的悬浮文本。
    • accelerator string (optional) - An Accelerator string.
    • icon (NativeImage | string) (optional)
    • enabled boolean (可选) - 如果为 false,菜单项将置灰,同时不可点击。
    • acceleratorWorksWhenHidden boolean (optional) macOS - default is true, and when false will prevent the accelerator from triggering the item if the item is not visible.
    • visible boolean (可选) - 如果为 false,菜单项将完全的隐藏。
    • checked boolean (可选) - 应仅为指定 checkboxradio 类型的菜单项。
    • registerAccelerator boolean (可选) Linux Windows - 如果为 false,快捷键不会在系统中注册,但是任然会显示。 默认值为 true。
    • sharingItem SharingItem (可选) macOS - 当 roleshareMenu时指定要分享的项
    • submenu (MenuItemConstructorOptions[] | Menu) (可选) - 应该是固定类型菜单submenu 如果指定 submenu ,可以省略 type: 'submenu' 如果该值不属于Menu,它将被函数Menu.buildFromTemplate自动转换。
    • id string (可选) - 单个菜单内唯一。 若定义,则可以通过 position 位置属性找到此选项
    • before string[] (optional) - Inserts this item before the item with the specified id. 如果引用值不存在,那么该菜单项会插在这个菜单的尾部。 这还意味着,菜单项应该被放置在与引用项相同的组中。
    • after string[] (optional) - Inserts this item after the item with the specified id. 如果引用值不存在,那么该菜单项会插在这个菜单的尾部。
    • beforeGroupContaining string[] (optional) - Provides a means for a single context menu to declare the placement of their containing group before the containing group of the item with the specified id.
    • afterGroupContaining string[] (optional) - Provides a means for a single context menu to declare the placement of their containing group after the containing group of the item with the specified id.
note

acceleratorWorksWhenHidden is specified as being macOS-only because accelerators always work when items are hidden on Windows and Linux. 该选项让用户可以选择关闭,因为这是本地 macOS 开发中的可能。

实例属性

以下为 MenuItem 实例的可用属性:

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

string 表明选项的子标签

MenuItem接收到点击事件后自动触发的方法 Function 。 调用方法为 menuItem.click(event, focusedWindow, focusedWebContents).

A Menu (可选) 指定子菜单

string 表示菜单项的类型 Can be normal, separator, submenu, checkbox, radio, header or palette.

note

header and palette are only available on macOS 14 and up.

一个 string 值(可选),如果设置,表示菜单项的角色。 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 (可选) 若存在则指向该项的快捷键

一个 Accelerator | null 值, 表示菜单项的 user-assigned accelerator 项。

[!NOTE] This property is only initialized after the MenuItem has been added to a Menu. 通过 Menu.buildFromTemplateMenu.append()/insert()。 初始化前访问将只返回 null

NativeImage | string (可选) 若设置, 则从对应的值中找选项图标

string 表明选项的子标签

string 表明选项的子标签

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

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

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

checkbox 菜单项将在选中时切换 checked 的开关属性。

单选菜单项 将返回单击时checked属性, 并将关闭同一菜单中所有相邻项的属性。

你可以为其他行为添加click函数。

一个 boolean 值 ,表示快捷键应用被系统注册或仅显示出来。

此属性可以动态更改。

SharingItem 表示了当 roleshareMenu 时要分享的项。

此属性可以动态更改。

number 表示一个项目的序列唯一ID。

多个选项组成一个 Menu