按照官方文档进行操作,执行 npm start
时提示不明白 start
是什么操作,在package.json内添加 start
定义:
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
preload.js
文件内容:
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
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}`, process.versions[type])
}
})
index.html
文件增加标签id:
We are using node <span id="node"></span>,
Chrome <span id="chrome"></span>,
and Electron <span id="electron"></span>.
再次执行,显示成功。