我正在尝试使用fs模块从目录中读取文件,并在Electron应用程序中使用id displayfiles在div中显示该文件。我不断收到错误消息:
min-width
我尝试了各种不同的方法来访问menu.js中主窗口的Web内容,但是我一直遇到未定义的错误。 我在menu.js中的代码如下所示: 无法读取未定义的属性“ getCurrentWebContents”
Cannot read property 'getCurrentWebContents' of undefined
这是我的index.html文件:
const electron = require('electron');
const dialog = electron.dialog;
const fs = require('fs');
const remote = require ("electron").remote;
const template=[
{
label: 'File',
submenu: [
{
label:'Open USB',
click () { dialog.showOpenDialog( {
properties: ['openDirectory']
},directorySelectorCallback)
}
},
{
label:'Exit',
click() {
}
},
]
},
{
label: 'Edit',
submenu: [
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
{role: 'pasteandmatchstyle'},
{role: 'delete'},
{role: 'selectall'}
]
},
{
label: 'View',
submenu: [
{role: 'reload'},
{role: 'forcereload'},
{role: 'toggledevtools'},
{type: 'separator'},
{role: 'resetzoom'},
{role: 'zoomin'},
{role: 'zoomout'},
{type: 'separator'},
{role: 'togglefullscreen'}
]
},
{
role: 'window',
submenu: [
{role: 'minimize'},
{role: 'close'}
]
},
]
function directorySelectorCallback(filenames) {
console.log("test:");
if (filenames && filenames.length > 0) {
console.log(filenames[0]);
fs.readdir(filenames[0], (err, files) => {
'use strict';
if (err) throw err;
//the files parameter is an array of the files and folders in the path we passed. So we loop through the array, printing each file and folder
for (let file of files) {
//the += after innerHTML means we are appending to the existing content
remote.getCurrentWebContents().executeJavaScript(`
document.getElementById("display-files").innerHTML += '<li> ${file} </li>'
`)
}
});
}
}
module.exports.template=template;
这是我的main.js文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rexnord</title>
</head>
<body>
<div ><ol id="display-files"></ol></div>
<script>
// You can also require other files to run in this process
require('./renderer.js')
require('./menu/menu.js')
</script>
</body>
</html>
我如何在menu.js文件中访问主窗口的Web内容,以便可以操纵html?