示例预览
在本节中,我们收集了一些新手通用的功能示例,可以让你快速实现需要的 Electron 应用。 每个例子都包含一个简单的、自成一体的示例应用程序。 运行这些示例的最简单方法是下载 Electron Fiddle。
一旦安装了Fiddle,就可以按下 "Open in Fiddle"按钮,然后会在下面找到以下代码样本:
- index.html
- main.js
- preload.js
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Hello World!</title> <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" /></head><body> <h1>Hello World!</h1> <p> We are using Node.js <span id="node-version"></span>, Chromium <span id="chrome-version"></span>, and Electron <span id="electron-version"></span>. </p></body></html>
const { app, BrowserWindow } = require('electron')const path = require('path')
function createWindow () { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { preload: path.join(__dirname, 'preload.js') } })
win.loadFile('index.html')}
app.whenReady().then(() => { createWindow()
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } })})
app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() }})
window.addEventListener('DOMContentLoaded', () => { const replaceText = (selector, text) => { const element = document.getElementById(selector) if (element) element.innerText = text }
for (const type of ['chrome', 'node', 'electron']) { replaceText(`${type}-version`, process.versions[type]) }})
#
How to...?You can find the full list of "How to?" in the sidebar. If there is something that you would like to do that is not documented, please join our Discord server and let us know!