如何在电子中传递命令行参数 [英] How to pass command line argument in electron
问题描述
我刚开始使用电子。当我使用 npm start
运行电子时,我对如何在 electron
中传递命令行参数存有疑问。
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.
在 Node.js
中,我正在使用: node server.js一二=三四个
命令提示符:
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);
});
在 Node.js
中正常工作。我需要知道如何使它在电子中工作。
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?
推荐答案
传递参数的方式相同,唯一需要注意的是电子路径。在 package.json
中,其编写的 npm
开始将执行 electron main.js
。因此,您将必须显式执行该命令,并使用正确的电子路径传递参数。即 ./ node_modules / .bin / electron
。然后命令将是
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
,您可以通过 main.js $ c中的
process.argv
访问这些参数$ c>
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。在您的主机中。 js定义了一个变量,例如
1.In your main.js define a variable like
global.sharedObject = {prop1: process.argv};
2。在您的应用程序中,只需包含remote并使用此 sharedObject
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。输出将为 [ argv1, argv2]
这篇关于如何在电子中传递命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!