【问题标题】:How can send and get data beteen Main.js and html.js in Election如何在 Election 中的 Main.js 和 html.js 之间发送和获取数据
【发布时间】:2021-11-05 05:23:09
【问题描述】:

如何在 Electron 中使用ipcRenderer 发送数据? 我使用此代码,但效果不佳。我无法将数据从 ma​​in.js 发送到 notes2 到 index.html

在 Main.js 中

   ipcMain.on('notes', function(event, data) {
      console.log('notes: ', data)
      win.webContents.send('notes2', "dddddddddddddd");
   });

【问题讨论】:

    标签: javascript electron electron-builder electron-packager


    【解决方案1】:

    Main.js

    const { app, BrowserWindow, ipcMain } = require('electron');
    const url = require('url')
    const path = require('path')
    let mainWindow;
    /**
     * Create a new window
     */
    const createWindow = () => {
       mainWindow = new BrowserWindow({
        webPreferences: {
          /** Enable node integration */
          nodeIntegration: true
        }
      });
    
      /** Open devTools */
      mainWindow.webContents.openDevTools();
    
      /** Load the index.html page */
         mainWindow.loadURL(url.format ({
          pathname: path.join(__dirname, 'index.html'),
          protocol: 'file:',
          slashes: true
       }))
    };
    
    /**
     * Initialize the application
     */
     
    const init = () => {
      /** Create app window */
      createWindow();
    
      /** Define channel name and message */
      const CHANNEL_NAME = 'main';
      const MESSAGE = 'tick';
        ipcMain.on(CHANNEL_NAME, (event, data) => {
        /** Show the request data */
        console.log(data);
          mainWindow.webContents.send(CHANNEL_NAME, "hhhhhhhhhhhhhhhh");
        /** Send a response for a synchronous request */
        event.returnValue = 'pong';
      });
      /** Send message every one second */
      
      setInterval(() => {
        mainWindow.webContents.send('another', MESSAGE);
      }, 1000);
      
    
      
    };
    
    /**
     * Run the app
     */
    app.on('ready', init);
    

    index.html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>IPC test app</title>
    
        <script>
            const { ipcRenderer } = require('electron');
    
            /** Define channel name */
            const CHANNEL_NAME = 'main';
    
            /** Create a processor for a button's click event */
            const clickButton = () => {
              /** Message to be sent */
              let message = 'ping';
    
              /** Show response for a sync IPC request */
              console.log(ipcRenderer.sendSync(CHANNEL_NAME, message));
            }
            
            
        /** Add IPC event listener */
          ipcRenderer.on(CHANNEL_NAME, (event, data) => {   
            console.log('hiiiiiiii ' , data);
          });
          
            /** Add IPC event listener */
          ipcRenderer.on('another', (event, data) => {  
            console.log('hello  : ' , data);
          });
          
          
        </script>
    </head>
    <body>
        <button onclick="clickButton()">Press me</button>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 2020-09-26
      • 2017-10-13
      • 1970-01-01
      • 2018-04-22
      • 2016-08-12
      • 2013-01-11
      • 2019-10-11
      • 1970-01-01
      相关资源
      最近更新 更多