How to pass command line argument in electron

前端 未结 1 910
北荒
北荒 2021-01-11 11:18

I just started using electron. I have a doubt about how to pass command line arguments in electron when I\'m using npm start to run electron.

1条回答
  •  太阳男子
    2021-01-11 12:00

    The way of passing arguments will be same, the only thing you have to take care is path of electron. In package.json its written npm start will perform electron main.js. So you will have to execute this command explicitly and pass arguments with "proper path of electron" i.e ./node_modules/.bin/electron. Then the command will be

    ./node_modules/.bin/electron main.js argv1 argv2
    

    and these arguments you can access by process.argv in main.js

    and If wish you to access these parameters in your app then there are following things to do :

    1.In your main.js define a variable like

    global.sharedObject = {prop1: process.argv};
    

    2.In your app just include remote and use this sharedObject

    const remote = require('electron').remote;
    const arguments = remote.getGlobal('sharedObject').prop1;
    
    console.log(arguments);
    

    3.Output will be ["argv1", "argv2"]

    0 讨论(0)
提交回复
热议问题