Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 电子与角电荷_Javascript_Angular_Typescript_Electron - Fatal编程技术网

Javascript 电子与角电荷

Javascript 电子与角电荷,javascript,angular,typescript,electron,Javascript,Angular,Typescript,Electron,我试着从angular调用一些electron api并对其进行响应。对于测试,我尝试隐藏块: electron.ts: import {app,BrowserWindow,BrowserWindowConstructorOptions, ipcMain} from 'electron' let window:any = null app.whenReady().then(()=>{ const options:BrowserWindowConstructorOptions = {

我试着从angular调用一些electron api并对其进行响应。对于测试,我尝试隐藏块:

electron.ts:

import {app,BrowserWindow,BrowserWindowConstructorOptions, ipcMain} from 'electron'
let window:any = null
app.whenReady().then(()=>{
    const options:BrowserWindowConstructorOptions = {
        width:300,
        height:600,
        title:"test",
        show:false,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation : false
        }
    }
    window = new BrowserWindow(options)

    window.loadFile('some path')
    window.webContents.on('did-finish-load',()=>{
        window.show()
        ipcMain.on('test',(value)=>{
           window.webContents.send('test2')
         
        })
    })
})
app-component.html:

<div *ngIf='__flag'><h1>Test</h1>
    <button (click)='clickFunction()'>Test button</button>
</div>

但只有当我点击两次按钮时,div才会删除。为什么会这样?我怎样才能解决这个问题?我需要隐藏块,然后单击按钮

您只是在设置事件侦听器(this.elect.ipcRenderer.on('test2'))将消息发送到主进程后,单击函数。因此,当来自主进程的调用到达时,侦听器尚未侦听。它在第二次单击时工作,因为侦听器已在第一次单击中配置,因此您将获得消息。 将事件侦听器移动到ngOnInit函数,它应该可以正常工作

ngOnInit(): void {
    this.elect.ipcRenderer.on('test2',()=>{
        this.__flag=false
        console.log(this.__flag);
    }); 
}
  
clickFunction(){
  this.elect.ipcRenderer.send('test');
}
ngOnInit(): void {
    this.elect.ipcRenderer.on('test2',()=>{
        this.__flag=false
        console.log(this.__flag);
    }); 
}
  
clickFunction(){
  this.elect.ipcRenderer.send('test');
}