[Electron] ipcMain 和 ipcRenderer

ipcMain

const { ipcMain } = require("electron");

ipcMain.handle("click-me", (_, msg) => {
  return "<----" + msg + "---->";
})

ipcRenderer

const { contextBridge, ipcRenderer } = require("electron");

contextBridge.exposeInMainWorld("electron", {
  platform: process.platform,
  clickMe: (msg) => ipcRenderer.invoke("click-me", msg)
});

App.vue

<script lang="ts" setup>
import {ref} from "vue";

const result = ref("")
const message = ref("")

async function handleClick() {
  result.value = await window.electron.clickMe(message.value)
}
</script>

<template>
  <input v-model="message"/>
  <button @click="handleClick">click me</button>
  <p>{{ result }}</p>
</template>

简单梳理

  1. 页面点击;
  2. ipcRenderer 发送;
  3. ipcMain 处理和返回;
  4. ipcRenderer 收取;
posted @ 2024-10-06 00:38  Himmelbleu  阅读(13)  评论(0编辑  收藏  举报