久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Electron 中使用 ipc 從渲染器設置全局變量

Using ipc in Electron to set global variable from renderer(在 Electron 中使用 ipc 從渲染器設置全局變量)
本文介紹了在 Electron 中使用 ipc 從渲染器設置全局變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

renderer.js

renderer.js

ipcRenderer.sendSync('setGlobal', 'globalVarName').varInner.varInner2 = 'result';

main.js

global.globalVarName = {
  varInner: {
    varInner2: ''
  },
  iWontChange: 'hi'
};

ipcMain.on('setGlobal', (event, arg) => {
  console.log(arg) // should print "result"
  // what goes here?
})

console.log(varInner2) // should print "result"

這樣的事情是否可能,即以這種方式設置globalVarNamevarInner2?其次,有沒有辦法對此進行優化,這樣我們就不必為每個全局變量重寫這個過程(即使用動態變量名的某種方式)?

Is something like this possible, namely setting the varInner2 of globalVarName in this manner? Secondly, is there a way to optimize this so we wouldn't have to rewrite this process for every global variable (i.e. some way to do this with dynamic variable names)?

感謝任何想法或解決方案,如果這是一個常識性問題,請見諒.

I appreciate any ideas or solutions, sorry if this is a common sense question.

推薦答案

使用IPC設置全局值.

當您只對讀取全局變量的值感興趣時,使用 getGlobal 非常有用.但是,我發現嘗試使用 getGlobal 分配或更改其值是有問題的.

Use IPC to Set the Global's Value.

Using getGlobal works great when you're only interested in reading the value of the global variable. However, I found that trying to assign or change its value using getGlobal to be problematic.

在我的例子中,我發現主進程上的全局變量并沒有真正改變.具體來說,在開發中刷新 Electron 窗口時,全局變量被設置回原來的值.這使得在發展中恢復狀態成為一個問題.

In my case, I found that the global variable on the Main process didn't actual change. Specifically, when refreshing the Electron window in development, the global variables were set back to their original value. This made restoring state in development an issue.

不確定這是否也發生在生產中,但我想它會發生,因此建立依賴于全局變量最新值的新流程將是有問題的.

Not sure if this also was occurring in production, but I imagine it would, so spinning up new processes that relied on up-to-date values of global variables would be problematic.

相反,我最終使用了 ipcMainipcRenderer 更詳細的方法.

Instead, I ended up using the more verbose method of ipcMain and ipcRenderer.

ma??in.js

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

ipcMain.on( "setMyGlobalVariable", ( event, myGlobalVariableValue ) => {
  global.myGlobalVariable = myGlobalVariableValue;
} );

renderer.js

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

// Set MyGlobalVariable.
ipcRenderer.send( "setMyGlobalVariable", "Hi There!" );

// Read MyGlobalVariable.
remote.getGlobal( "MyGlobalVariable" ); // => "Hi There!"

這篇關于在 Electron 中使用 ipc 從渲染器設置全局變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 国产日韩欧美中文 | 91视视频在线观看入口直接观看 | 久久国产成人 | 视频一区二区三区中文字幕 | 亚洲一区中文字幕在线观看 | 粉色午夜视频 | 黄色毛片免费 | 国产精品jizz在线观看老狼 | 暖暖成人免费视频 | 日本黄色激情视频 | 爱草在线 | 日韩视频 中文字幕 | 日韩在线一区视频 | 国产aⅴ爽av久久久久久久 | 国产999精品久久久久久 | 日本一区二区影视 | 亚洲一区二区电影在线观看 | 久草在线青青草 | 男人天堂av网站 | 国产精品美女久久久久久免费 | 亚洲成人av在线 | 国产一极毛片 | 亚洲综合久久久 | 亚洲一区二区视频 | 欧美天堂一区 | 欧美综合久久 | 欧美专区日韩 | 国产免费一区二区 | 国产一二三区在线 | 亚洲va国产日韩欧美精品色婷婷 | 亚洲国产成人精品在线 | 欧美日韩第一页 | 国产精品一二三区 | 青青草原精品99久久精品66 | 日本久久久久久久久 | 亚洲色图插插插 | 999免费视频| 一级a性色生活片久久毛片 午夜精品在线观看 | 三级黄色片在线 | 久久久久久久一区二区 | 亚洲欧美国产精品久久 |