MenuItem
类:MenuItem
🌐 Class: MenuItem
将项目添加到本地应用菜单和上下文菜单。
进程:主进程
🌐 Process: Main
请参阅 Menu 了解示例。
🌐 See Menu for examples.
Electron 内置的类不能在用户代码中被继承。 欲了解更多信息,请参见 常见问题。
new MenuItem(options)
acceleratorWorksWhenHidden 被指定为仅适用于 macOS,因为在 Windows 和 Linux 上,当菜单项被隐藏时,快捷键始终有效。向用户提供此选项,是为了让他们可以关闭它,因为在原生 macOS 开发中这是可行的。
实例属性
🌐 Instance Properties
MenuItem 实例上可用的属性如下:
🌐 The following properties are available on instances of MenuItem:
menuItem.id
string 表示该物品的唯一标识符。此属性可以动态更改。
🌐 A string indicating the item's unique id. This property can be
dynamically changed.
menuItem.label
string 表示物品的可见标签。
🌐 A string indicating the item's visible label.
menuItem.click
当菜单项接收到点击事件时触发的 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).
event键盘事件focusedWindowBaseWindowfocusedWebContents网页内容
menuItem.submenu
一个 Menu(可选),包含菜单项的子菜单(如果有的话)。
🌐 A Menu (optional) containing the menu
item's submenu, if present.
menuItem.type
string 表示项目的类型。可以是 normal、separator、submenu、checkbox、radio、header 或 palette。
🌐 A string indicating the type of the item. Can be normal, separator, submenu, checkbox, radio, header or palette.
header 和 palette 仅在 macOS 14 及以上版本可用。
menuItem.role
一个可选的 string,表示该项目的角色(如果已设置)。可以是 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 或 windowMenu
🌐 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
menuItem.accelerator
如果设置了,Accelerator | null 表示该物品的快捷键。
🌐 An Accelerator | null indicating the item's accelerator, if set.
menuItem.userAccelerator 只读 macOS
🌐 menuItem.userAccelerator Readonly macOS
一个 Accelerator | null,表示该菜单项的 用户分配快捷键。
🌐 An Accelerator | null indicating the item's user-assigned accelerator for the menu item.
该属性仅在将 MenuItem 添加到 Menu 后才会被初始化。可以通过 Menu.buildFromTemplate 或 Menu.append()/insert() 添加。在初始化之前访问,只会返回 null。
menuItem.icon
NativeImage | string(可选)表示该物品的图标,如果已设置的话。
🌐 A NativeImage | string (optional) indicating the
item's icon, if set.
menuItem.sublabel
string 表示该物品的子标签。
🌐 A string indicating the item's sublabel.
menuItem.toolTip macOS
一个 string,表示物品的悬停文本。
🌐 A string indicating the item's hover text.
menuItem.enabled
boolean 表示该项是否已启用。此属性可以动态更改。
🌐 A boolean indicating whether the item is enabled. This property can be
dynamically changed.
menuItem.visible
boolean 表示该项是否可见。此属性可以动态更改。
🌐 A boolean indicating whether the item is visible. This property can be
dynamically changed.
menuItem.checked
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.
menuItem.registerAccelerator
boolean 表示加速器是否应注册到系统中或仅显示出来。
🌐 A boolean indicating if the accelerator should be registered with the
system or just displayed.
该属性可以动态更改。
🌐 This property can be dynamically changed.
menuItem.sharingItem macOS
当 role 为 shareMenu 时,SharingItem 表示要共享的项目。
🌐 A SharingItem indicating the item to share when the role is shareMenu.
该属性可以动态更改。
🌐 This property can be dynamically changed.
menuItem.commandId
number 表示物品的顺序唯一标识符。
🌐 A number indicating an item's sequential unique id.
menuItem.menu
物品所属的 Menu 。
🌐 A Menu that the item is a part of.