久久久久久久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 服務器時的歡迎消息)
主站蜘蛛池模板: 欧美性视频在线 | 欧美一道本 | 亚洲男人在线 | 精品国产精品三级精品av网址 | 婷婷视频网 | 夜夜嗨av一区二区三区网页 | 欧美日韩一区二 | 日韩在线视频网站 | 伊人精品 | 97色在线 | 午夜美女福利 | 久久久国产精品人人片 | 三级在线播放 | a视频在线免费观看 | 一级免费片 | 欧美亚洲在线观看 | 欧美三级韩国三级日本三斤在线观看 | 亚洲视频在线观看免费 | 黄视频网站在线观看 | 一色桃子av| 9.1成人看片 | 亚洲精品第一页 | 久久国产99 | 中文字幕在线观看第一页 | 成人综合婷婷国产精品久久 | av在线播放免费 | 久久夜色精品国产欧美乱极品 | 精品国产一区二区在线观看 | 91一级片 | 91精品91久久久中77777 | 又大又黄又爽 | 免费成人黄色 | 成人午夜视频在线观看 | 亚洲国产成人在线 | 亚洲综合在线视频 | 国产一区在线看 | 亚洲成人久久久 | 国产一区精品在线观看 | 久久av资源| 日产精品久久久一区二区 | 91av免费|