2016-11-28 181 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) 
    } 
}