問題描述
表單 html 和提交事件是渲染器"的一部分.提交的數據應該在主進程中可用.提交表單并使該數據在 main.js 中可訪問的正確方法是什么?
The form html and submit event is part of the "renderer". The submitted data should be available in the main process. What's the proper way to submit the form and make that data accessible in main.js ?
我應該簡單地使用遠程"模塊將數據從 main.js 傳遞給函數還是有更好的方法?
Should I simply use the "remote" module to pass the data to a function from main.js or is there a better approach?
推薦答案
我們使用服務(Angular)來處理窗口中的表單數據.如果需要,然后通知 remote
.
We use a service (Angular) to process form data in a window. Then notify the remote
, if needed.
從您的 renderer
您可以將數據發送到 ipc
,然后在您的 main.js
中捕獲此事件和傳遞的表單數據:
From your renderer
you can send data to the ipc
, then in your main.js
you catch this event and the passed form data:
// renderer.js
let ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.send('submitForm', formData);
// main.js
ipcMain.on('submitForm', function(event, data) {
// Access form data here
});
<小時>
您還可以從 main.js
將消息發送回 renderer
.
同步:
// main.js
ipcMain.on('submitForm', function(event, data) {
// Access form data here
event.returnValue = {"any": "value"};
});
或異步:
// main.js
ipcMain.on('submitForm', function(event, data) {
// Access form data here
event.sender.send('formSubmissionResults', results);
});
// renderer.js
ipcRenderer.on('formSubmissionResults', function(event, args) {
let results = args.body;
});
這篇關于在 Electron 中處理表單的正確方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!