Electron和单纯的vue单页面结合通讯

基本操作就不说了,跑起来gitdemo

# 克隆示例项目的仓库
$ git clone https://github.com/electron/electron-quick-start

# 进入这个仓库
$ cd electron-quick-start

# 安装依赖并运行
$ npm install && npm start

然后通讯Api地址

h1主程序

// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain} = require('electron')
const path = require('path')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration:true
    }
  })

  // and load the index.html of the app.
  // mainWindow.loadFile('index.html')
  mainWindow.loadURL('http://localhost:8080/#/index?key=1574085961805')
  // mainWindow.loadURL('https://www.baidu.com/')

  // Open the DevTools.
  mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) createWindow()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
ipcMain.on('send_message_load', (event, arg) => {
    console.log(arg) // prints "ping"
    event.reply('asynchronous-reply', 'pong')
})
setTimeout(()=>{
    mainWindow.webContents.send('asynchronous-message','{a:b:c:12}')
},5000)

最主要一个正常的webpack -cli项目通讯
跑起来的操作我就不说了百度。。。

和vue通讯的结合方式 const { ipcRenderer } = require('electron')

直接引用会报错fs等不存在 和

解决方式

1.
image.png
  1. vue结合
created() {
//            let electron = window.require('electron');
//            console.log('ipcRenderer...', electron)
            console.log(ipcRenderer)
            ipcRenderer.on('asynchronous-message', function (event, message) {
                console.log(event)
                console.log(message)
            })
            ipcRenderer.send('send_message_load', '{ad:"1"}')
        },

上姿势图


image.png

一直想解决来着,github的vue-Electron升级之后到处是bug无奈自己看官网手动操作了,如果对大家有帮助,请给个小赞,谢谢

你可能感兴趣的:(Electron和单纯的vue单页面结合通讯)