問題描述
我有一個可以打開不同窗口的 Electron 應用.
I have an Electron app that can open different windows.
在應用啟動時,應用會打開一組窗口(加載相同的 HTML 和 JS 文件),但使用參數來更改每個窗口顯示的信息.
On app launch the app open a set of window(s) (that load the same HTML and JS files) but with params to change each window displayed infos.
例子:
app.on('ready', async () => {
...
// open window for stuff 1
win1 = new BrowserWindow({
width: 1024,
height: 728
});
win1.loadURL(`file://${__dirname}/app/app.html?id=1`);
// open window for stuff 2
win2 = new BrowserWindow({
width: 1024,
height: 728
});
win2.loadURL(`file://${__dirname}/app/app.html?id=2`);
顯然在 file://路徑中傳遞參數不起作用.我在 Electron 文檔或 Internet 上的其他地方找不到明確的解決方案來將渲染的窗口調整為參數.
Obviously passing params in file:// path doesn't work. I can't find a clear solution in Electron documentation or elsewhere on Internet to condition my rendered window to a param.
我可能可以在窗口準備好后使用 IPC 通信,但在我只想將一個變量傳遞給我的子視圖之前,這似乎有點太復雜了.
I can probably use IPC communication after window ready but it seems a little bit too complicated until I just want pass a variable to my child view.
附:: 老實說,我的應用程序是用 React/Redux 構建的,我想傳遞給視圖的參數是用于監聽該視圖的 redux 存儲鍵.
P.S. : to be totally honest my application is built with React/Redux and the param I want to pass to view is the redux store key to listen for this view.
推薦答案
根據 atom 源代碼,查詢字符串方法是一種非常簡單的可靠方法,尤其是當我們只需要傳遞一個唯一的字符串參數時:
According atom source code the query string method is a reliable way to do that very simply, especially when we only need to pass a unique string param:
// main process
win1.loadURL(`file://${__dirname}/app/app.html?id=${id}`);
// rendered process
console.log(global.location.search);
https://github.com/electron/electron/issues/6504
這篇關于如何將參數從主進程傳遞到 Electron 中的渲染進程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!