html转换成窗体electron,使用electron的菜单栏实现页面切换

如下图所示,我们想要实现这样的效果

6574496b2e9eb1a312d144e90dbbafdb.gif

在上图所示的这个示例项目中,他的页面切换是这样实现的:

通过mainWindow.webContents.send()函数实现了主进程到渲染进程的消息传递

主进程的代码如下

const sendMenuEvent = async (data) => {

mainWindow.webContents.send('change-view', data)

}

const template = [

{

label: app.name,

submenu: [

{

label: 'Home',

accelerator: 'CommandOrControl+H',

click() {

sendMenuEvent({ route: '/' })

console.log(mainWindow.webContents)

},

},

{ type: 'separator' },

{ role: 'minimize' },

{ role: 'togglefullscreen' },

{ type: 'separator' },

{ role: 'quit', accelerator: 'Alt+F4' },

],

},

{

role: 'help',

submenu: [

{

label: 'Get Help',

role: 'help',

accelerator: 'F1',

click() {

sendMenuEvent({ route: '/help' })

},

},

{

label: 'About',

role: 'about',

accelerator: 'CommandOrControl+A',

click() {

sendMenuEvent({ route: '/about' })

},

},

],

},

]

渲染进程的代码如下

new Vue({

el: '#app',

router,

store,

render: (h) => h(App),

})

// to avoild accesing electorn api from web app build

if (window && window.process && window.process.type === 'renderer') {

const { ipcRenderer } = require('electron')

ipcRenderer.on('change-view', (event, data) => {

console.log(data)

if (data.route) {

router.push(data.route)

}

})

}

可以看到,在接受到主进程传来的change-view消息后,渲染进程通过vue-router进行页面的切换

不过在更新的electorn版本中,该API已经被废弃,我们可以看一下源码中关于这个函数的说明:

/**

* Send an asynchronous message to the renderer process via `channel`, along with

* arguments. Arguments will be serialized with the Structured Clone Algorithm,

* just like `postMessage`, so prototype chains will not be included. Sending

* Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.

*

* > **NOTE**: Sending non-standard JavaScript types such as DOM objects or special

* Electron objects is deprecated, and will begin throwing an exception starting

* with Electron 9.

*

* The renderer process can handle the message by listening to `channel` with the

* `ipcRenderer` module.

*

An example of sending messages from the main process to the renderer process:

*/

send(channel: string, ...args: any[]): void;

那么就有两种解决方案

一种是继续使用老版本的 "electron": "^8.3.1"

另一种就是通过URL实现页面的切换

我们这里直接通过URL实现页面的切换,主进程的代码如下

const template = [

{

label: '当前位置: 首页',

},

{

type: 'separator'

},

{

label: '首页',

submenu: [

{

label: '首页',

click() {

changeUrl({route: '/'}, '首页')

}

}

]

}

]

function changeUrl(data, label) {

mainWindow.loadURL(`${process.env.WEBPACK_DEV_SERVER_URL}/#${data.route}`)

}

上图中所示的方式十分简单,也易于理解,大多数场景下是够用的

本文地址:https://blog.csdn.net/a772316182/article/details/107381987

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现自定义菜单栏,可以使用 Electron 提供的 Menu 模块。以下是一个使用 Electron Vite Vue 实现自定义菜单栏的步骤: 1. 在 Vue 组件中引入 Electron 的 remote 模块,用于获取主进程的 Menu 对象。 ```javascript import { remote } from 'electron' const Menu = remote.Menu ``` 2. 在 Vue 组件的生命周期钩子函数中创建菜单项,可以使用 Menu.buildFromTemplate 方法创建菜单项的数组。 ```javascript created() { const template = [ { label: '文件', submenu: [ { label: '新建', accelerator: 'CmdOrCtrl+N', click: this.handleNew }, { label: '打开', accelerator: 'CmdOrCtrl+O', click: this.handleOpen }, { type: 'separator' }, { label: '保存', accelerator: 'CmdOrCtrl+S', click: this.handleSave }, { label: '另存为', accelerator: 'Shift+CmdOrCtrl+S', click: this.handleSaveAs }, { type: 'separator' }, { label: '退出', accelerator: 'CmdOrCtrl+Q', click: this.handleQuit } ] }, { label: '编辑', submenu: [ { label: '撤销', accelerator: 'CmdOrCtrl+Z', role: 'undo' }, { label: '重做', accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' }, { type: 'separator' }, { label: '剪切', accelerator: 'CmdOrCtrl+X', role: 'cut' }, { label: '复制', accelerator: 'CmdOrCtrl+C', role: 'copy' }, { label: '粘贴', accelerator: 'CmdOrCtrl+V', role: 'paste' }, { label: '全选', accelerator: 'CmdOrCtrl+A', role: 'selectAll' } ] } ] const menu = Menu.buildFromTemplate(template) Menu.setApplicationMenu(menu) } ``` 3. 在 Vue 组件中实现菜单项的点击事件。 ```javascript methods: { handleNew() { // 新建文件 }, handleOpen() { // 打开文件 }, handleSave() { // 保存文件 }, handleSaveAs() { // 另存为文件 }, handleQuit() { // 退出应用程序 } } ``` 这样就可以在 Electron Vite Vue 应用程序中实现自定义菜单栏了。需要注意的是,菜单项的点击事件可以调用主进程中的方法,例如使用 ipcRenderer 发送消息给主进程,让主进程执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值