electron vue 打开新窗口

1、主进程

background.js文件

const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:8080` : `file://${__dirname}/index.html`

//事件名      openWindow是事件名
ipcMain.on('openWindow', function () {
  //调用 BrowserWindow打开新窗口
  const win2 = new BrowserWindow({
      width:400,
      height:300,
      webPreferences: {
        devTools: true,
        nodeIntegration: true,
        enableRemoteModule: true,
        contextIsolation: false,
      },
  })
  //test页面路由
  win2.loadURL(winURL+"#/test");

  win2.webContents.openDevTools();

  win2.on('closed',()=>{
      win2=null;
  })
 
  
})

2、渲染进程

Index.vue页面

<template>
  <div class="index">
      <button @click="openwin">打开新窗口</button>
  </div>
</template>

<script>
const { ipcRenderer } = window.require("electron");
    
export default {
  name: "index",
  components: {},
  data() {
    return {};
  },
  methods: {
    openwin(){
        //触发事件名
        ipcRenderer.send("openWindow");
    }
  },
};
</script>
<style lang="scss" scoped>
</style>

Test.vue页面

<template>
  <div class="test">
    我是test页面
  </div>
</template>

<script>
export default {
  name: "test",
  components: {},
  data() {
    return {};
  },
  methods: {
    
  },
};
</script>
<style lang="scss" scoped>
</style>

3、路由

{
  path: '/test',
  name: 'test',
  component: () => import('@/views/Test.vue')
},

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论
为了在electron-vue应用程序中打开一个新的子窗口并渲染自己开发的vue组件页面,可以按照以下步骤进行操作: 1.在主进程中创建一个新的BrowserWindow实例,该实例将作为子窗口打开。 ```javascript // main.js const { BrowserWindow } = require('electron') let childWindow function createChildWindow() { childWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, contextIsolation: false, enableRemoteModule: true, }, }) childWindow.loadURL(process.env.WEBPACK_DEV_SERVER_URL + '/#/child') childWindow.on('closed', () => { childWindow = null }) } ``` 2.在vue组件中使用vue-router进行页面跳转,以便在子窗口中渲染自己的组件页面。 ```javascript // router/index.js import Vue from 'vue' import Router from 'vue-router' import Child from '@/components/Child' Vue.use(Router) export default new Router({ routes: [ { path: '/child', name: 'Child', component: Child, }, ], }) ``` 3.在渲染进程中使用contextBridge将openWindow方法暴露给vue组件,以便在vue组件中调用该方法打开子窗口。 ```javascript // renderer.js const { contextBridge, ipcRenderer } = require('electron') const openWindow = (param) => { ipcRenderer.invoke('on-open-event', param) } contextBridge.exposeInMainWorld('myApi', { openWindow }) ``` 4.在vue组件中调用openWindow方法打开子窗口。 ```javascript // Child.vue <template> <div class="child"> <h1>This is a child window</h1> <button @click="openChildWindow">Open Child Window</button> </div> </template> <script> export default { methods: { openChildWindow() { window.myApi.openWindow() }, }, } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豪先生5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值