electron electron-vue创建子窗口

只能通过主线程创建窗口

1、配置路由,在router/index.js中配置好窗口对应的路由

2、在main/index.js主线程中创建窗口
	function openVip()
		var xx = new BrowserWindow({
		    width: 400,
		    height: 550,
		    x:n,  位移
		    y:n,  位移
		    parent: mainWindow, 
		    frame:false,  是否显示默认工具栏
		    webPreferences: {
		        nodeIntegration: true
		    }
		})
		  xx.loadURL(winURL+'#/router配置的path路径');
		  xx.on('closed', () => { vipWin = null })
	}

3、操作子窗口
	开启:
		通过主线程和渲染进程的通信,在主线程中对窗口进行自定义操作
			ipcMain.on('openVip', e =>
			  openVip()
			);
	关闭放大缩小等:
		方式一:通过主进程和渲染进程通信,在主进程关闭
		方式二:在当前进程的窗口关闭,remote.getCurrentWindow().close();

代码示例:
主进程

import { app, BrowserWindow,ipcMain } from 'electron'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

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

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 670,
    useContentSize: true,
    width: 1020,
    frame:false,
    webPreferences:{
      // devTools:false
    }

  })

  mainWindow.loadURL(winURL)

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

// 开启vip新窗口

function openVip()
{
  var vipWin = new BrowserWindow({
    width: 400,
    height: 550,
    parent: mainWindow, // win是主窗口
    // frame:false,
    webPreferences: {
        nodeIntegration: true
    }
})
  vipWin.loadURL(winURL+'#/vip');
  // vipWin.loadURL("https://bilibili.com/")
  // vipWin.loadFile('../renderer/pages/openVip/index.vue')
  vipWin.on('closed', () => { vipWin = null })
}

ipcMain.on('openVip', e =>
  openVip()
);


app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */

渲染进程:

showWindow()
{
    ipcRenderer.send('openVip')
}

路由:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: require('@/pages/home').default,
      meta:{
        showTop:true
      }
    },
    {
      path: '/vip',
      name: 'vip',
      component: require('@/pages/openVip').default,
      meta:{
        showTop:false
      }
    },
  ]
})

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值