电子处理input

我最近开始用Electron弄湿自己的脚。 我真的很喜欢它背后的原理,但是我发现做一些事情有点困惑。

例如,你如何处理用户input? 我有一个main.js和一个BrowserWindow指向一个本地的HTML文件(包含一些用户设置与input字段)。

如何在HTML表单提交时访问这些数据(到同一个文件或另一个文件)?

main.js

const {app, BrowserWindow} = require('electron') let win function createWindow () { win = new BrowserWindow({width: 800, height: 600}) win.loadURL('file://' + __dirname + '/index.html') // Emitted when the window is closed. win.on('closed', () => { win = null }) // Open the DevTools. // win.webContents.openDevTools() } app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', () => { if (win === null) { createWindow() } }) // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here. //Start the main window app.on('ready', createWindow) 

的index.html

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="" method="post"> <input type="text" name="test-1"> </form> </body> </html> 

使用Electron,node.js不会像典型的Web应用程序场景中那样充当路由的web服务器。 而不是发送请求路由,你会创build一个单一的页面应用程序使用像Angular,React,Knockout等JavaScript框架。在这一点上,你不再需要处理路由。 您将直接在页面中将“提交”单击事件绑定到JavaScript函数,并从那里处理input。

您可以从页面的JavaScript上下文中执行所有操作,您可以从node.js主进程上下文中执行此操作。 例如,如果您需要从页面访问文件系统,则可以使用远程模块访问node.js本机API。

例如:

 // Gain access to the node.js file system api function useNodeApi() { const remote = require('electron').remote; const fs = remote.require('fs'); fs.writeFile('test.txt', 'Hello, I was written by the renderer process!'); } 

我很less遇到这种情况,我需要将控制权交还给主stream程来完成某件事情。 一旦BrowserWindow启动,任何你可能需要做的事情都可以从渲染器进程完成。 这几乎消除了像通过http提交表单post这样的事情的需要。