16

我刚开始使用电子。electron我对在使用npm start运行电子时如何传递命令行参数有疑问。

Node.js我正在使用:node server.js one two=three four 命令提示符:

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

Node.js工作。我需要知道如何在电子中进行这项工作。

有人可以为此提供解决方案吗?

4

1 回答 1

18

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"]

于 2016-06-06T11:53:28.340 回答