简体   繁体   中英

Call function on other file from menu Electron.js

I got a custom menu set up in Electron, and I want to call a function - written in another .js file - from it.

Code:

Main.js:

const { Menu } = require('electron');

var menu = Menu.buildFromTemplate([
{
    label: 'Foo Menu',
    submenu: [
    {
        label: 'Foo',
        click() {
            // What do I put here?
        },
        accelerator: 'CmdOrCtrl+F'
    }
}]);
Menu.setApplicationMenu(menu);

src/scripts/index.js:

// The function that I want to be called
function foo() {
    console.log('foo');
}

You have to export the function:

// index.js
function foo() {}
module.exports = foo;

and import into the other file with:

const foo = require('./index.js');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM