在ipcRenderer.on事件处理程序上设置全局变量在电子上运行离子不起作用

时间:2018-09-11 08:15:09

标签: ionic-framework electron ipc render main

这是我在home.ts中的代码,用于从主进程接收值

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
declare var electron : any;

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  arguments: any="ping";
  constructor(public navCtrl: NavController) {
  }
  ionViewDidLoad() {
    electron.ipcRenderer.send("test_channel","ping");
    electron.ipcRenderer.on("test_channel", function (err,arg) {
      this.arguments=arg; // receive new value "pong" from main.js
      console.log("Message received from electron: "+arg); // works fine
    });
    console.log("Message received from electron: "+this.arguments); //does not work, still default value
  };
}

这已添加到我在main.js中的代码中,并且可以从渲染过程接收事件

var ipcMain = require('electron').ipcMain;
mainWindow.webContents.openDevTools();
ipcMain.on("test_channel",function(err,arg){
  console.log(err);
  console.log("Received message: "+arg);
  global.sharedObj = {prop1: arg};
  console.log("Sending message back!");
  // Send message back!
  mainWindow.webContents.send("test_channel",arg+'yeah');
}) 

这被添加到我的index.html中,以使其可以运行离子型

  <script>
    const electron = require('electron');
  </script>

0 个答案:

没有答案