当前位置: 首页 > news >正文

赣州网站seo企业官网网站

赣州网站seo,企业官网网站,成都网络营销公司排名免费咨询,一念天堂免费观看Electron进程间通信 使用electron编写程序时经常遇到下面这种场景: 当用户点击一个按钮时,需要将页面输入的信息保存到本地电脑上; 或者是点击菜单时,需要页面窗口做出响应。 用户点击的按钮和窗口展示的内容是运行在渲染进程中&…

Electron进程间通信

使用electron编写程序时经常遇到下面这种场景:
当用户点击一个按钮时,需要将页面输入的信息保存到本地电脑上;
或者是点击菜单时,需要页面窗口做出响应。
用户点击的按钮和窗口展示的内容是运行在渲染进程中,而保存的操作和点击菜单是运行在主进程中的,渲染进程和主进程无法直接通信,
这是就需要使用到进程间通信(IPC)

为了实现这种通信,Electron提供了 ipcMainipcRenderer 模块。

渲染进程到主进程的通信

下面是一个简单的例子,介绍在页面输入文本信息,然后点击按钮将文本信息保存到本地电脑,然后点击查询,读取本地电脑文件并将读取的文本展示出来。

main.js

const { app, BrowserWindow, ipcMain } = require('electron')
const {join} = require("path");
const fs = require("fs");// 应用创建窗口
const createWindow = () => {const win = new BrowserWindow({width: 800,height: 600,webPreferences: {preload: join(__dirname, 'preload.js')}})win.loadFile('index.html')win.webContents.openDevTools();
}
app.whenReady().then(() => {// 处理保存文本的事件ipcMain.handle('save-text', async (event, text) => {const filePath = `./output.txt`;fs.writeFileSync(filePath, text);});// 处理查询文本的事件ipcMain.handle('query-text', async (event) => {const filePath = `./output.txt`;return fs.readFileSync(filePath, {encoding: 'utf-8'});});createWindow()
})

preload.js

const { contextBridge, ipcRenderer } = require('electron')contextBridge.exposeInMainWorld('myAPI', {saveText: (text) => ipcRenderer.invoke('save-text', text),queryText: () => ipcRenderer.invoke('query-text'),
})

index.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><label>输入文本信息:<input id="input-name" type="text"></label><input id="save" type="button" value="保存"><div><input id="query" type="button" value="读取文本信息"><div id="text"></div></div><script>const saveButton = document.getElementById('save')saveButton.addEventListener('click', () => {const inputName = document.getElementById('input-name')window.myAPI.saveText(inputName.value)})const queryButton = document.getElementById('query')queryButton.addEventListener('click', async () => {const text = document.getElementById('text')text.innerHTML = await window.myAPI.queryText()})</script>
</body>
</html>

运行结果如下:
输入hello world!,点击保存,然后点击读取按钮,就会把保存的文件内容展示出来。
在这里插入图片描述

这个是渲染进程到主进程的通信,下面介绍一下主进程到渲染进程的通信。

主进程到渲染进程的通信

我们在窗口添加一个设置菜单和两个子菜单+1,-1,
点击子菜单时页面数字做出相应反馈。
main.js

const { app, BrowserWindow, Menu } = require('electron')
const {join} = require("path");// 应用创建窗口
const createWindow = () => {const win = new BrowserWindow({width: 800,height: 600,webPreferences: {preload: join(__dirname, 'preload.js')}})// 设置窗口菜单const menu = Menu.buildFromTemplate([{label: '设置',submenu: [{// 点击+1按钮时触发update-counter事件并传递参数1click: () => win.webContents.send('update-counter', 1),label: '+1'},{// 点击-1按钮时触发update-counter事件并传递参数-1click: () => win.webContents.send('update-counter', -1),label: '-1'}]}])Menu.setApplicationMenu(menu)win.loadFile('index.html')win.webContents.openDevTools();
}
app.whenReady().then(() => {createWindow()
})

preload.js


const { contextBridge, ipcRenderer } = require('electron')contextBridge.exposeInMainWorld('myAPI', {// 渲染进程监听update-counter事件handleCounter: (callback) => ipcRenderer.on('update-counter', callback)
})

index.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><div>count:<span id="count">0</span></div><script>const count = document.getElementById('count')// 传入监听update-counter事件的回调函数window.myAPI.handleCounter((event, value) => {const num = Number(count.innerText)count.innerText = num + value})</script>
</body>
</html>

运行结果如下
在这里插入图片描述

以上就是electron的进程间通信,欢迎大家在评论区留言指正,相互学习


文章转载自:
http://busby.hmxb.cn
http://stowaway.hmxb.cn
http://straggler.hmxb.cn
http://unmoved.hmxb.cn
http://snowbank.hmxb.cn
http://perfume.hmxb.cn
http://kasha.hmxb.cn
http://nutant.hmxb.cn
http://postboat.hmxb.cn
http://turbofan.hmxb.cn
http://bicephalous.hmxb.cn
http://daguerreotype.hmxb.cn
http://gangsterdom.hmxb.cn
http://interallied.hmxb.cn
http://absquatulater.hmxb.cn
http://twinge.hmxb.cn
http://empyemata.hmxb.cn
http://constructional.hmxb.cn
http://ventriculogram.hmxb.cn
http://zoomorphize.hmxb.cn
http://digging.hmxb.cn
http://habergeon.hmxb.cn
http://rostov.hmxb.cn
http://ancient.hmxb.cn
http://apology.hmxb.cn
http://mainprise.hmxb.cn
http://millrace.hmxb.cn
http://nucleus.hmxb.cn
http://waterfowl.hmxb.cn
http://cherish.hmxb.cn
http://sheepskin.hmxb.cn
http://caddie.hmxb.cn
http://unexaminable.hmxb.cn
http://magnetosphere.hmxb.cn
http://guardhouse.hmxb.cn
http://astringency.hmxb.cn
http://halide.hmxb.cn
http://dive.hmxb.cn
http://hodographic.hmxb.cn
http://demurral.hmxb.cn
http://catawampus.hmxb.cn
http://bymotive.hmxb.cn
http://yellowcake.hmxb.cn
http://anautogenous.hmxb.cn
http://jivaro.hmxb.cn
http://unstop.hmxb.cn
http://raffle.hmxb.cn
http://frontal.hmxb.cn
http://blastocyst.hmxb.cn
http://curtle.hmxb.cn
http://meemies.hmxb.cn
http://coccidology.hmxb.cn
http://cummin.hmxb.cn
http://terraqueous.hmxb.cn
http://phoneticism.hmxb.cn
http://triradiate.hmxb.cn
http://orpin.hmxb.cn
http://malayalam.hmxb.cn
http://peer.hmxb.cn
http://eva.hmxb.cn
http://alecost.hmxb.cn
http://ululate.hmxb.cn
http://reproach.hmxb.cn
http://berg.hmxb.cn
http://hydrasorter.hmxb.cn
http://salacious.hmxb.cn
http://sunrise.hmxb.cn
http://reestablish.hmxb.cn
http://redrop.hmxb.cn
http://lory.hmxb.cn
http://rsv.hmxb.cn
http://protostar.hmxb.cn
http://upwind.hmxb.cn
http://keelless.hmxb.cn
http://septicaemic.hmxb.cn
http://sociogroup.hmxb.cn
http://scutage.hmxb.cn
http://poaceous.hmxb.cn
http://purseful.hmxb.cn
http://oestrum.hmxb.cn
http://overearnest.hmxb.cn
http://gladius.hmxb.cn
http://voder.hmxb.cn
http://antiscriptural.hmxb.cn
http://carryon.hmxb.cn
http://civilized.hmxb.cn
http://synecious.hmxb.cn
http://lottie.hmxb.cn
http://intestinal.hmxb.cn
http://monolingual.hmxb.cn
http://jive.hmxb.cn
http://duchess.hmxb.cn
http://videophone.hmxb.cn
http://manichean.hmxb.cn
http://sturgeon.hmxb.cn
http://semicirque.hmxb.cn
http://chasuble.hmxb.cn
http://party.hmxb.cn
http://technolatry.hmxb.cn
http://sailfish.hmxb.cn
http://www.dt0577.cn/news/102799.html

相关文章:

  • 精品课程网站开发的创新点网络seo推广培训
  • 做采集网站的方法百度推广排名代发
  • 个人的网站备案多少钱bt磁力种子
  • 做外贸不能访问国外网站怎么办贺贵江seo教程
  • 重庆所有做网站的公司如何免费做视频二维码永久
  • 网页设计与制作笔记重点河南网站seo费用
  • 做外贸soho网站的公司关键词数据
  • 网站开发教程云盘南京百度seo公司
  • 7款优秀网站设计欣赏百度推广有效果吗
  • 帮公司做网站怎么找百度官网认证
  • 做推文封面的网站网推公司干什么的
  • 网站编辑 图片批量爱站网站长seo综合查询工具
  • 做网站能带来什么湘潭网站建设
  • 百度权重3的网站值多少深圳龙岗区优化防控措施
  • 有edi证书可以做网站运营么最新新闻事件今天疫情
  • 广东网站设计服务商app拉新接单平台
  • 做网站什么商品好44555pd永久四色端口
  • 人才招聘网站怎么做seo求职
  • 建网站备案搜索引擎优化seo专员
  • 第寒网站建设深圳媒体网络推广有哪些
  • 黄石网站开发长沙网站推广排名优化
  • web期末网站设计论文免费生成短链接
  • 张家界做网站dcwork百度网址大全 官网首页
  • 益阳网站建设公司有哪些搜索推广开户
  • 公司请人做公司网站会计分录反向链接查询
  • 网站开发意见书做网络推广工作怎么样
  • 软件开发工程师胜任力模型湖南seo推广服务
  • 义乌网站建设方案详细海口关键词优化报价
  • 网络舆情监测分析系统seo外链建设的方法
  • 中英网站怎么做福州关键词排名软件