[英]TypeError: x is not a function in Node.js
I'm developing an Electron application and I aim to 'split up' index.js
(main process) file. 我正在开发一个Electron应用程序,目的是“拆分”
index.js
(主进程)文件。 Currently I have put my menu bar-related and Touch Bar-related code into two separate files, menu.js
and touchBar.js
. 目前,我已经将与菜单栏相关的代码和与触摸栏相关的代码放入了两个单独的文件
menu.js
和touchBar.js
。 Both of these files rely on a function named redir
, which is in index.js
. 这两个文件都依赖于名为
redir
的函数,该函数位于index.js
。 Whenever I attempt to activate the click
event in my Menu Bar - which relies on redir
- I get an error: 每当我尝试激活菜单栏中的
click
事件(依赖于redir
,都会出现错误:
TypeError: redir is not a function
. TypeError: redir is not a function
。 This also applies to my Touch Bar code. 这也适用于我的触摸条形码。
Here are my (truncated) files: 这是我的(被截断的)文件:
index.js
const { app, BrowserWindow } = require('electron'); // eslint-disable-line
const initTB = require('./touchBar.js');
const initMenu = require('./menu.js');
...
let mainWindow; // eslint-disable-line
// Routing + IPC
const redir = (route) => {
if (mainWindow.webContents) {
mainWindow.webContents.send('redir', route);
}
};
module.exports.redir = redir;
function createWindow() {
mainWindow = new BrowserWindow({
height: 600,
width: 800,
title: 'Braindead',
titleBarStyle: 'hiddenInset',
show: false,
resizable: false,
maximizable: false,
});
mainWindow.loadURL(winURL);
initMenu();
mainWindow.setTouchBar(initTB);
...
}
app.on('ready', createWindow);
...
menu.js
const redir = require('./index');
const { app, Menu, shell } = require('electron'); // eslint-disable-line
// Generate template
function getMenuTemplate() {
const template = [
...
{
label: 'Help',
role: 'help',
submenu: [
{
label: 'Learn more about x',
click: () => {
shell.openExternal('x'); // these DO work.
},
},
...
],
},
];
if (process.platform === 'darwin') {
template.unshift({
label: 'Braindead',
submenu: [
...
{
label: 'Preferences...',
accelerator: 'Cmd+,',
click: () => {
redir('/preferences'); // this does NOT work
},
}
...
],
});
...
};
return template;
}
// Set the menu
module.exports = function initMenu() {
const menu = Menu.buildFromTemplate(getMenuTemplate());
Menu.setApplicationMenu(menu);
};
My file structure is simple - all three files are in the same directory. 我的文件结构很简单-所有三个文件都在同一目录中。
Any code criticisms are also welcome; 也欢迎任何对代码的批评; I've spent hours banging my head trying to figure all this out.
我花了好几个小时才想尽办法解决这个问题。
redir
it is not a function, because you're exporting an object, containing a redir
property, which is a function. redir
不是函数,因为要导出的对象包含redir
属性,该对象是函数。
So you should either use: 因此,您应该使用:
const { redir } = require('./index.js');
Or export it this way 或以这种方式导出
module.exports = redir
When you do: module.exports.redir = redir;
当您执行以下操作时:
module.exports.redir = redir;
You're exporting: { redir: [Function] }
您正在导出:
{ redir: [Function] }
You are exporting 您正在出口
module.exports.redir = redir;
That means that your import 这意味着您的导入
const redir = require('./index');
is the exported object. 是导出的对象。
redir
happens to be one of its keys. redir
恰好是其关键之一。 To use the function, use 要使用该功能,请使用
const redir = require('./index').redir;
or destructure directly into redir 或直接分解为redir
const { redir } = require('./index');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.