如何使用参数运行电子应用程序?
问题描述:
我的应用程序是一个浏览器窗口加载本地页面index.html电子。
我打电话给“npm run start”脚本运行“electron main.js”,应用程序打开并载入html。
我可以添加一个参数给脚本,将不同的html文件加载到BrowserWindow中?如何使用参数运行电子应用程序?
在main.js文件中的代码是:
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
webPreferences:{
webSecurity:false
},
fullscreen : false });//, alwaysOnTop : true , kiosk : true })
mainWindow.setMenu(null);
// and load the index.html of the app.
let url = `file://${__dirname}/index.html`; \\ index.html should be determined by argument passed at start.
mainWindow.loadURL(url,loadOptions);
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
答
传递参数的方式将是一样的,你要照顾的唯一事情就是电子的路径。在package.json
其书面npm
开始将执行electron main.js
。所以你必须明确地执行这个命令并且传递“正确的电子路径”参数,即./node_modules/.bin/electron
。那么该命令将
./node_modules/.bin/electron main.js argv1 argv2
和这些参数可以通过process.argv
在main.js
访问。如果希望您能够在您的应用程序访问这些参数则有以下事情要做:
1 。在你的main.js这样定义
global.sharedObject = {prop1: process.argv}
可变
2.In您的应用程序只包括远程和使用本sharedObject
var remote = require('electron').remote,
arguments = remote.getGlobal('sharedObject').prop1;
console.log(arguments);
3.输出将["argv1", "argv2"]
来源:https://discuss.atom.io/t/how-to-pass-command-arguments-in-electron/17247
是'process.argv'不填充? –
是的,但我如何传递我的参数,如-html = index2.html –