前言
Vue+electron项目,当在渲染进程中调用remote时导致页面空白,控制台报错TypeError: fs.existsSync is not a function
错误原因
渲染进程没有集成node环境因此无法使用fs包,以及无法使用require关键字。
解决
参考官方文档
1:在主进程中开启远程对象
let win = new BrowserWindow({
show:false,
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: true,
contextIsolation: false,
//使用远程模块必须开启此处
enableRemoteModule:true
}
})
2:在渲染进程中引入remote
const {remote} = window.require('electron')
注:使用以下这种方式,无法获取remote。
const {remote} = require('electron')
问题解决。