如何使用参数运行电子应用程序? [英] How to run an electron app with arguments?

查看:65
本文介绍了如何使用参数运行电子应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用是带有 BrowserWindow 的电子,加载了本地页面index.html.
我将 npm run start 称为运行 electron main.js 的脚本,应用程序打开并加载了html.
我可以在脚本中添加一个参数,以将不同的html文件加载到 BrowserWindow 中吗?

My app is electron with a BrowserWindow loading a local page index.html.
I call npm run start a script to run electron main.js , the app opens and the html loaded.
Can I add an argument to the script that will load different html file into the BrowserWindow ?

在main.js文件中,代码为:

In the main.js file the code is :

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;
  });
}

推荐答案

要将命令行参数传递给电子应用程序:

To pass the command line arguments to electron app:

./node_modules/.bin/electron main.js --arg1=value --arg2=value

可以在 main.js 中进行检索:

import { app } from "electron";
app.commandLine.getSwitchValue("arg1");
app.commandLine.getSwitchValue("arg2");

这篇关于如何使用参数运行电子应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆