本文介绍了如何运行带参数的电子应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用是 electron,带有一个 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;
});
}
推荐答案
将命令行参数传递给electron app:
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");
这篇关于如何运行带参数的电子应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!