electron主進程與渲染進程進行通信

主進程向渲染進程發送消息

  • 主進程發送消息(main=>index.js)

      mainWindow = new BrowserWindow({
          height: 750,
          width: 1100,
          center: true, // 是否出現在屏幕居中的位置
          useContentSize: true,
          frame:false,//設置爲 false 時可以創建一個無邊框窗口
          resizable:true,//窗口是否可以改變尺寸
          autoHideMenuBar:true,//是否隱藏菜單欄
          backgroundColor:'#fff',// 窗口的背景顏色爲十六進制值
          titleBarStyle:'hidden',//窗口標題欄的樣式
          webPreferences:{//網頁功能的設置
            nodeIntegration: true,//是否集成node
            // devTools:false,//是否開啓 DevTools
            // webSecurity: false//是否禁用同源策略(上線刪除)
          }
      });
      
      //發送消息
      mainWindow.webContents.send('changeWin',1);
    
  • 渲染進程接收消息(home.vue)

      const { ipcRenderer } = require('electron');
      
      ipcRenderer.on('changeWin',(event,arg)=>{
        //這裏是主進程傳過來的消息
        console.log(arg);
      })
    

渲染進程向主進程發送消息

  • 渲染進程發送消息(about.vue)

      const { ipcRenderer } = require('electron');
    
      ipcRenderer.send('sendmsg',1);
    
  • 主進程接收消息(main=>index.js)

      import { ipcMain } from 'electron';
      
      ipcMain.on('sendmsg',(event,arg)=>{
          //這裏是渲染進程發送來的消息
          console.log(arg)
      });
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章