久久久久久久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 服務器時的歡迎消息)
主站蜘蛛池模板: 国产欧美日韩综合精品一 | 久久精品久久综合 | 亚洲精品第一页 | 操操日| 成人亚洲性情网站www在线观看 | 成人在线观看免费观看 | cao在线 | 亚洲一区二区视频 | 国产九九精品视频 | 国产精品日韩在线观看一区二区 | 久久伊人精品一区二区三区 | 国精品一区二区 | 亚洲欧美综合精品久久成人 | 天天操狠狠操 | 美女张开腿露出尿口 | 国产偷自视频区视频 | 国产一区| 毛片在线免费 | 91在线看片| 国产1区2区在线观看 | 一区天堂| 欧美一区二区大片 | 大学生a级毛片免费视频 | 91资源在线播放 | 久久精品国产免费看久久精品 | 日韩在线中文字幕 | 成人三级在线播放 | 亚洲精品视频在线 | 国产精品久久久久久久久久免费看 | 久久国产精品一区 | 免费久久视频 | 免费国产一区二区 | 午夜欧美a级理论片915影院 | 日韩一级| 国产精品视频免费观看 | 亚洲女人天堂网 | 99热热热 | 美日韩免费 | 在线一区二区三区 | 日韩免费网站 | 午夜ww|