2016-11-28 38 views
0

需求:電子渲染過程cannt使用ipcRenderer.send()

渲染過程需要將數據發送到主進程。

我的代碼:

//index.js (renderer process) 
const {ipcRenderer} = require('electron') 

class WebWindow { 
    constructor() { 

    ... 

    setInterval(() => { 
     this.foo() 
    }, 2000) 

    // or 

    let that = this 
    setInterval(function() { 
     thar.foo() 
    }, 2000) 


    } 

    foo() { 
    data = {} 
    ipcRenderer.send('async-cookies', data) 
    } 
} 

問題 我得到的錯誤:

Uncaught Exception: 
TypeError: Cannot read property 'send' of undefined 
    at Function.eval 

Semms cannt中的setInterval使用IPC?

我該怎麼辦..

謝謝!

+0

使用箭頭函數時,您不需要播放這個/那個/自我重新聲明遊戲,並且您的代碼不顯示該聲明? –

+0

我編輯過。這兩個代碼我都試過了.. – steve

回答

0

沒有必要在類WebWindow中定義另一個函數,也不需要在箭頭函數中重新聲明這個

//index.js (renderer process) 
const {ipcRenderer} = require('electron') 

class WebWindow { 
    constructor() { 

    setInterval(() => { 
     ipcRenderer.send('async-cookies', data) 
    }, 2000) 
    } 
}