How to pass command line argument in electron

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

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.

In node.js i am using: node server.js one two=three four command prompt for :

 var arguments = process.argv.slice(2);arguments .forEach(function(val,index, array) {   console.log(index + ': ' + val); }); 

In node.js is working. I need to know how can I make this work in electron. Can someone please give a solution for this?

回答1:

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 linclude remote and use this sharedObject

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

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



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