Where to place electron-cookies?

扶醉桌前 提交于 2019-12-12 04:03:11

问题


Here it basically says: In your app's renderer code, just require this package....

I don't know where to place it. When i put it in main.js my app just won't start anymore. I installed it correctly since its in my node_modules folder. Any ideas?

My main.js:

'use strict';

const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

function createWindow () {

  require('electron-cookies');
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600,nodeIntegration:false});

  // and load the index.html of the app.
  mainWindow.loadURL('file://' + __dirname + '/index.html');

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

  // Emitted when the window is closed.
  mainWindow.on('closed', function() {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow();
  }
});

As said -> this main.js doesn't work because of the require('electron-cookies')


回答1:


First you need to understand the difference between the browser process and the renderer process, reading the Electron Quick Start, and a brief overview of the Electron architecture should help.

Now, main.js runs in the browser process, index.html runs in the renderer process, so to use electron-cookies you have to load it either directly or indirectly in your index.html. The direct approach would be:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script>
    require('electron-cookies');
  </script>
</head>
<body>
</body>



来源:https://stackoverflow.com/questions/35431750/where-to-place-electron-cookies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!