2017-06-15 3 views
0

電子メインメニューからレンダラースクリプトにonclickでアクセスする必要があります。私は上記のpreferences.jsまたはupdate.jsを呼び出すようにしようとしている電子メインメニューからレンダラースクリプトにアクセスする

{ 
     label: 'Checking for Update', 
     click: function() { 
      require('./update') 
     } 
} 

......................... 

{ 
     label: `Preferences...`, 
     click: function() { 
      require('./preferences') 
     } 
} 

:私は電子メインメニューに以下を追加しました。両方のスクリプトはindex.htmlから直接呼び出すとうまくいきますし、appのウィンドウからonclickを経由してもアプリのメニューからは呼び出せません。メインアプリメニューでクリックするとそのわずか

Uncaught Exception: 
TypeError: Cannot read property 'app' of undefined 
    at IncomingMessage.res.on (update.js:20:36) 
    at emitOne (events.js:96:13) 
    at IncomingMessage.emit (events.js:188:7) 
    at IncomingMessage.Readable.read (_stream_readable.js:381:10) 
    at flow (_stream_readable.js:761:34) 
    at resume_ (_stream_readable.js:743:3) 
    at _combinedTickCallback (internal/process/next_tick.js:74:11) 
    at process._tickCallback (internal/process/next_tick.js:98:9) 

update.jsでerroring:20人のルックスを次のように:メインメニュー処理には、リモートはありません

const appname = electron.remote.app.getName() 

。リモートモジュールはレンダラにしか属していないので、これは私にとっては妥当なエラーと思われます。

しかし、私はこれを簡単に修正し、appメニューから一度呼び出されたスクリプトを動作させる方法はあまりよく分かりません。

このような何かが、仕事ができる

答えて

0

const {app} = (process.type === 'renderer' ? require('electron').remote : require('electron')) 

私はあなたのコードのメインとレンダラ証拠を作るのを助けるべきだと思います。もちろん、残りのコードをそれに応じて変更する必要があります。 const appname = electron.remote.app.getName()はちょうど私が、次のようにそれを回避することができましたconst appname = app.getName()

+0

うん、私は似たような実装しました。私は自分のチェックロジックを、よりクリーンなので、あなたの提案に従って 'process.type'で置き換えます。ありがとう!承認済みと表示されます。 –

+0

'const {app、dialog} = require( 'electron')。リモート|| require( 'electron') 'も動作します。 – RoyalBingBong

+0

short&sweet! –

0

なる:

const electron = require('electron') 
const remote = electron.remote  

if (remote) { // remote will be 'undefined' when calling from the main process    
    const app = remote.app 
    const dialog = remote.dialog 
    const window = remote.getCurrentWindow() 
} else { 
    const app = electron.app 
    const dialog = electron.dialog 
    const window = electron.getCurrentWindow() 
} 
関連する問題