electron窗口和主进程通信ipc
来源:互联网 发布:苹果4s如何变成4g网络 编辑:程序博客网 时间:2024/06/16 17:19
前端和后台通信ipc
const electron = require('electron')const { app } = electronconst ipc = require('electron').ipcMainconst { BrowserWindow } = electronlet winipc.on('getMsg', (sys, msg) => { console.log(msg) //接收窗口传来的消息})app.on('ready', createWindow)app.on('window-all-cloased', () => { if (process.platform !== 'drawin') { app.quit() }})app.on('activate', () => { if (win === null) { createWindow() }})function createWindow() { win = new BrowserWindow({ width: 600, height: 400 }) win.loadURL(`file://${__dirname}/app/index.html`) // win.webContents.openDevTools() //开启调试工具 win.on('close', () => { win = null }) win.on('resize', () => { win.reload() }) win.on('closed', () => { win = null })}
- index.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>myapp</title></head><body> <h1>electron-app-3</h1> <input type="" name="" id="ipt" value=""> <button type="button" onclick="connectMain()">和主进程通信</button> <script> const ipc = require('electron').ipcRenderer; var ipt = document.getElementById('ipt') function connectMain() { console.log('index.html', ipt.value); ipc.send('getMsg', ipt.value) } </script></body></html>
阅读全文