import { ipcMain } from "electron";
ipcMain.handle("open-win", (_, arg) => {
const childWindow = new BrowserWindow({
webPreferences: {
preload:path.join(__dirname, 'preload.mjs'),
nodeIntegration: true,
contextIsolation: false,
},
});
childWindow.loadFile(path.join(RENDERER_DIST, 'index.html'),{
hash: arg
});
});
import { ipcRenderer } from 'electron'
window.ipcRenderer = ipcRenderer
/**
* 新建窗口
* @param path 路由地址
*/
export function openWindow(path: string) {
window.ipcRenderer.invoke("open-win", path);
}
export default {
openWindow
};
<script setup lang="ts">
const router = useRouter()
import electronUtils from "@/utils/electron";
const toUser = () => {
router.push('/userManage')
}
const toNote = () => {
router.push('/func/noteManage')
}
const toLogin = () => {
router.push('/login')
}
const toOpenLogin = () => {
electronUtils.openWindow('/login');
}
</script>
<template>
<div class="container">
<a @click="toUser()">用户管理</a>
<a @click="toNote()">语录管理</a>
<a @click="toLogin()">去登录</a>
<a @click="toOpenLogin()">创建新窗口</a>
</div>
</template>
import { ipcMain } from "electron";
export const initIpc = () => {
// 同步通信监听
ipcMain.on("eventSync", (e, data: string) => {
setTimeout(() => {
e.returnValue = "主进程同步响应:" + data;
}, 1000);
});
// 异步通信监听
ipcMain.handle("eventAsync", (e, data: string) => {
return "主进程异步响应:" + data;
});
// 异步通信一次
ipcMain.handleOnce("eventAsyncOnce", (e) => {
console.log("异步通信一次!",e);
});
};
import {initIpc} from './ipc'
//初始化initIpc
initIpc()
{
path: '/',
component: Layout,
hidden: false,
redirect: 'noredirect',
children: [
...
{
path: 'userManage/detail',
component: () => import('@/views/userManage/detail.vue'),
name: 'detail',
meta: { title: '用户详情', icon: 'user' },
hidden: true
}
]
}
<script setup lang="ts">
import { ipcRenderer } from "electron";
function toSyncSaveUserInfo() {
const result = ipcRenderer.sendSync("eventSync", "同步保存");// 同步通信测试
console.log(result);
}
function toAsyncSaveUserInfo() {
const result = ipcRenderer.invoke("eventAsync", "异步保存");// 异步通信测试
console.log(result);
}
function toAsyncOnceSaveUserInfo() {
const result = ipcRenderer.invoke("eventAsyncOnce", "异步保存一次");// 异步通信测试一次
console.log(result);
}
</script>
<template>
<a @click="toSyncSaveUserInfo()">同步保存</a>
<a @click="toAsyncSaveUserInfo()">异步保存</a>
<a @click="toAsyncOnceSaveUserInfo()">异步保存一次</a>
</template>
<style scoped>
</style>
<script setup lang="ts">
import electronUtils from "@/utils/electron";
const router = useRouter()
const toIndex = () => {
router.push('/')
}
const toNote = () => {
router.push('/func/noteManage')
}
const toEditUserInfo = () => {
electronUtils.openWindow('/userManage/detail');
}
</script>
<template>
<a @click="toIndex()">首页</a>
<a @click="toNote()">语录管理</a>
<a @click="toEditUserInfo()">编辑用户详情</a>
</template>
<style scoped>
</style>
点击保存用户信息,子进程向主进程通信
同步保存、异步保存、异步保存一次,其中异步保存一次后主进程移除监听,再次点击保存报错
ipcMain.handle("open-win", (_, arg) => {
const childWindow = new BrowserWindow({
webPreferences: {
preload:path.join(__dirname, 'preload.mjs'),
nodeIntegration: true,
contextIsolation: false,
},
});
let count = 100
setInterval(() => {
if (childWindow&&childWindow.webContents) {
childWindow?.webContents.send("to-child-count", '金额:'+count++);
}
}, 1000);
childWindow.webContents.openDevTools();
childWindow.loadFile(path.join(RENDERER_DIST, 'index.html'),{
hash: arg
});
});
// 监听主进程信息
function toChildCount(e:any,data:string){
console.log(data)
}
onMounted(()=>{
ipcRenderer.on("to-child-count",toChildCount);
console.log("监听主进程信息")
});
onUnmounted(()=>{
console.log('移除监听主进程信息')
ipcRenderer.removeListener("to-child-count",toChildCount);
});
//双向通信
ipcMain.handle("eventBothWay", (e, data: string) => {
e.sender.send("eventBothWay", "主进程发送" + data);
});
function eventBothWay(e:any,data:string){
console.log("子进程监听的信息:",data);
}
function toBothWay(){
ipcRenderer.invoke("eventBothWay","异步发给主进程的消息");
}
onMounted(()=>{
ipcRenderer.on("eventBothWay",eventBothWay);
// ipcRenderer.on("to-child-count",toChildCount);
// console.log("监听主进程信息")
});
onUnmounted(()=>{
ipcRenderer.removeListener("eventBothWay",eventBothWay);
// console.log('移除监听主进程信息')
// ipcRenderer.removeListener("to-child-count",toChildCount);
});
<a @click="toBothWay()">双向通信</a>
点击双向通信,可以控制台获取双向信息打印
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务