1.安装
npm install @electron/remote
2.搜索index.js中的webPreferences在其中加入enableRemoteModule:true
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule:true
}
3.再在下方加入初始化代码 loadUrl之上
const remoteMain = require("@electron/remote/main")
remoteMain.initialize()
remoteMain.enable(mainWindow.webContents)
4.再使用
const remote = require('@electron/remote');
const { dialog } = remote;
dialog.showOpenDialog({
title: '请选择你喜欢的照片',
//默认路径,默认选择的文件
defaultPath: 'default.jpg',
//过滤文件后缀
filters: [{
name: 'img',
extensions: ['jpg', 'png']
}],
//打开按钮
buttonLabel: '打开按钮文字',
//回调结果渲染到img标签上
}).then(result => {
let image = document.getElementById('images')
image.setAttribute("src", result.filePaths[0])
}).catch(err => {
console.log(err)
})