1. 程式人生 > 其它 >electron 子渲染程序獲取fs時報錯

electron 子渲染程序獲取fs時報錯

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.