【问题标题】:Electron ipc communication电子ipc通讯
【发布时间】:2020-07-27 10:48:57
【问题描述】:

我正在开发一个电子应用程序,并尝试使用 ipc 通信来调用我的 index.js 文件中的一个函数,该函数包含身份验证系统。我需要在ipcMain.on("login", (event, data) => {}) 事件中包含此功能。但遗憾的是函数launch 没有工作,我在控制台中没有任何错误。所以我假设有一种不同的方法。任何帮助将不胜感激

我的 index.js 文件:

const { app, BrowserWindow, ipcMain } = require('electron')


function createWindow() {
  // Create the browser window.
  var mainWindow = new BrowserWindow({
    title: "AstroLauncher",
    width: 1000,
    height: 660,
    
    webPreferences: {
      nodeIntegration: true
    }
    
  })
  mainWindow.setMenuBarVisibility(false)
  mainWindow.loadFile("home.html");
  return mainWindow;

}



app.whenReady().then(() => {
   const mainWindow = createWindow()


    app.on('activate', function () {
        if(BrowserWindow.getAllWindows.length === 0) createWindow()
    })

app.on('window-all-closed', function () {
    if(process.platform !== 'darwin') app.quit()
})

ipcMain.on("login", (event, data) => {
    

    Authenticator.getAuth(data.u, data.p).then(() => {
    event.sender.send('done')
    mainWindow.loadFile("home.html");   

    ipcMain.on('launch', () => {
      function launch() {
        alert('launched!');
      }
    })


 
}).catch((err) => {
   event.sender.send("err", { er: err })
    })
})

}).catch(err => console.log(err))

还有我的 home.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">.
    <script src="./index.js"></script>
 </head>
<body>
  <input type="button" value="Launch!" id="lanBTN" >

<script>
            const ipc = require('electron').ipcRenderer


    document.querySelector('#lanBTN').addEventListener("click", () => {
      ipc.send('launch')
    })
   
    </script>
</body>
</html>

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:
      ipcMain.on('launch', () => {
         function launch() {
            alert('launched!');
          }
         launch() ;
     })
    

    我想你忘了调用 launch() 或者你可以这样

      ipcMain.on('launch', () => {
            alert('launched!');
     })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-11
      • 2022-11-10
      • 2013-06-11
      • 1970-01-01
      • 2020-10-25
      • 2019-07-06
      • 2010-09-12
      • 2011-08-18
      相关资源
      最近更新 更多