博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

electron 子渲染进程获取fs时报错

Posted on 2021-06-16 15:09  阿浪在写BUG  阅读(557)  评论(1)    收藏  举报

electron主进程可以获取fs但是渲染进程不行要想获取fs必须使用在实例化window时声明上

 1 const { app, BrowserWindow } = require('electron');
 2 const path = require('path');
 3 
 4 // Handle creating/removing shortcuts on Windows when installing/uninstalling.
 5 if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
 6   app.quit();
 7 }
 8 
 9 const createWindow = () => {
10   // Create the browser window.
11   const mainWindow = new BrowserWindow({
12     width: 800,
13     height: 600,
14     webPreferences:{
15       nodeIntegration: true,  //完全使用nodeapi
16       contextIsolation:false,  //由于新版electron把这个默认属性改为true 官给出解释大概意思是保证渲染进程太容易访问主进程要保证安主进程全性隔离
17     }
18   });
19 
20   // and load the index.html of the app.
21   mainWindow.loadFile(path.join(__dirname, 'index.html'));
22 
23   // Open the DevTools.
24   mainWindow.webContents.openDevTools();
25 };
26 
27 // This method will be called when Electron has finished
28 // initialization and is ready to create browser windows.
29 // Some APIs can only be used after this event occurs.
30 app.on('ready', createWindow);
31 
32 // Quit when all windows are closed, except on macOS. There, it's common
33 // for applications and their menu bar to stay active until the user quits
34 // explicitly with Cmd + Q.
35 app.on('window-all-closed', () => {
36   if (process.platform !== 'darwin') {
37     app.quit();
38   }
39 });
40 
41 app.on('activate', () => {
42   // On OS X it's common to re-create a window in the app when the
43   // dock icon is clicked and there are no other windows open.
44   if (BrowserWindow.getAllWindows().length === 0) {
45     createWindow();
46   }
47 });
48 
49 // In this file you can include the rest of your app's specific main process
50 // code. You can also put them in separate files and import them here.