在我的电子应用中
exports.sayWord = function(){
console.log("some word")
};
main.js中的现在,在renderer.js
,我有
const main = require('./main.js');
但是当我运行应用程序并打开devtools时,我有错误:
Uncaught TypeError: Cannot read property 'on' of undefined
at Object.<anonymous> (/home/sean/elecapp/main.js:47:4)
at Object.<anonymous> (/home/sean/elecapp/main.js:70:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/sean/elecapp/renderer.js:20:14)
第47行是:
app.on('ready', createWindow);
但这没有意义,因为窗口是创建的,所以显然电子知道app
和createWindow
是什么。我怀疑这个问题与我需要main.js
的事实有关,因为我将函数sayWord
放在其他文件中,当我要求时,没有什么是错的。
答案 0 :(得分:1)
您收到错误的原因是main-process和renderer-process无权访问相同的模块。例如,可以通过app
直接在主进程中访问const {app} = require("electron")
,但在渲染器中,您只能通过const {app} = require("electron").remote
访问代理对象。但是你不应该使用remote.app
来解决你的问题。如果你要修改main.js脚本以在main-process和renderer-process上运行,你可能会创建一个创建新窗口的循环!
您应该将sayWord
外包给其他文件。如果您打算在main和渲染器之间发送数据,那么我建议改为使用ipcMain和ipcRenderer。
答案 1 :(得分:-1)
如果我理解你这是你的第一个电子项目并且你不能创建index.html?
这是我使用的main.js代码。它会将index.html加载到与start网站相同的文件夹中。你不需要在index.html中导入main.js
const {app, BrowserWindow, Tray, globalShortcut} = require('electron')
// this should be placed at top of main.js to handle setup events quickly
if (handleSquirrelEvent(app)) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
return;
}
const packager = require('electron-packager');
const path = require('path');
const url = require('url');
const mysql = require('mysql');
const loop = require('serial-loop');
const simpleTimestamp = require('simple-timestamp');
// 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 win
function createWindow () {
win = new BrowserWindow({
width: 980,
height: 620,
resizable: true,
maximizable: true,
center: true,
webPreferences: {
nodeIntegration: true
} // webPreferences
}) // win = new BrowserWindow({
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
// win.webContents.openDevTools()
// mit zurück taste kann man auch in app zurück gehen
win.on('app-command', (e, cmd) => {
// Navigate the window back when the user hits their mouse back button
if (cmd === 'browser-backward' && win.webContents.canGoBack()) {
win.webContents.goBack()
}
})
// Emitted when the window is closed.
win.on('closed', () => {
// 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.
win = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS 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', () => {
// On macOS 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 (win === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
function handleSquirrelEvent(application) {
if (process.argv.length === 1) {
return false;
}
const ChildProcess = require('child_process');
const path = require('path');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
const spawn = function(command, args) {
let spawnedProcess, error;
try {
spawnedProcess = ChildProcess.spawn(command, args, {
detached: true
});
} catch (error) {}
return spawnedProcess;
};
const spawnUpdate = function(args) {
return spawn(updateDotExe, args);
};
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
// Optionally do things such as:
// - Add your .exe to the PATH
// - Write to the registry for things like file associations and
// explorer context menus
// Install desktop and start menu shortcuts
spawnUpdate(['--createShortcut', exeName]);
setTimeout(application.quit, 1000);
return true;
case '--squirrel-uninstall':
// Undo anything you did in the --squirrel-install and
// --squirrel-updated handlers
// Remove desktop and start menu shortcuts
spawnUpdate(['--removeShortcut', exeName]);
setTimeout(application.quit, 1000);
return true;
case '--squirrel-obsolete':
// This is called on the outgoing version of your app before
// we update to the new version - it's the opposite of
// --squirrel-updated
application.quit();
return true;
}
};