2016-09-06 5 views
0

私のアプリケーションは、ローカルページindex.htmlをロードするBrowserWindowを持つ電子です。
"electron main.js"を実行するスクリプトを "npm run start"と呼びます。アプリが開き、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; 
 
    }); 
 
}

+1

がprocess.argv'が取り込まれていない 'ですか? –

+0

はい、どのように私の引数を渡すのですか?-html = index2.html –

答えて

2

引数を渡す方法は同じになり、あなたが世話をする必要が唯一のものは、電子のパスです。 package.jsonに書かれたnpm開始はelectron main.jsを実行します。したがって、このコマンドを明示的に実行し、引数に "適切な電子パス"、つまり./node_modules/.bin/electronを渡す必要があります。その後、コマンドは

./node_modules/.bin/electron main.js argv1 argv2 

になり、あなたがmain.js

process.argvでアクセスし、アプリでこれらのパラメータにアクセスすることができたい場合はすることができますこれらの引数は、次に行うには、次のものがあります。

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.Outputは["argv1", "argv2"]

ソースになります。https://discuss.atom.io/t/how-to-pass-command-arguments-in-electron/17247

関連する問題