【问题标题】:Electron renderer not invoked未调用电子渲染器
【发布时间】:2021-07-22 03:43:15
【问题描述】:

我正在学习 electron,学习了 electron-quick-start 并按照教程对 index.html、main.js 和 renderer.js 进行了一些修改 https://blog.logrocket.com/handling-interprocess-communications-in-electron-applications-like-a-pro/

index.html

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

  <title>Electron-Process-Comm</title>
  <link rel="stylesheet" href="style.css">
  <meta charset="UTF-8">
  <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
</head>
<body>
<h2 id="mainWindowTitle">I am the main window</h2>
<button id="sendSyncMsgBtn">Ping Main Process</button>
<p id="syncReply">Chilling for response</p>

<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>

ma​​in.js

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
const ipcMain = require('electron').ipcMain

console.log('Hello')

ipcMain.on('sync-message', (event, arg) => {
  console.log('IPCMain')
  event.returnValue = 'Message Recieved!'
})

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')
}

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

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

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

renderer.js

const electron = require('electron')
const ipc = electron.ipcRenderer

const syncMsgBtn = document.querySelector('#sendSyncMsgBtn')

console.log('Hello renderer')

syncMsgBtn.addEventListener('click', () => {
  console.log('Renderer')
  const reply = ipc.sendSync('sync-message', 'Sent from main Window')
  const message = `Synchronous message reply: ${reply}`
  document.querySelector('#syncReply').innerHTML = message
})

const asyncMsgBtn = document.querySelector('#sendAsyncMsgBtn')

asyncMsgBtn.addEventListener('click', () => {
  ipc.send('async-message', 'Async message baby')

})

现在,尽管 renderer.js 包含在 index.html 中,但它永远不会被调用。控制台日志没有出现,并且按钮单击没有响应。谁能告诉我我做错了什么?

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:

    代码看起来不错,应该在运行时执行。您可能正在查看错误的控制台?日志语句应该出现在渲染器窗口的控制台中。要打开开发者工具,您可以按Cmd + Opt + I (macOS) 和 Ctrl + Shift + I (Windows)。

    您可以在their documentation 中找到有关调试 Electron 应用程序的更多信息,在 Chrome's documentation 中找到开发者工具。

    【讨论】:

    • 谢谢,但是按下窗口上的按钮必须显示更改权。它没有。我在 Visual Studio 代码 Windows 10 上运行它。main.js 的控制台日志显示正常。
    • 渲染器控制台中是否出现任何错误?
    • 抱歉,我在哪里可以找到 Visual Studio 代码中的渲染器控制台? Ctrl + Shift + I 没有打开任何东西。
    • 我已经用两个文档链接更新了我的答案。当您打开渲染器的窗口时,您应该能够打开它们。您还可以以编程方式打开开发工具。见electronjs.org/docs/tutorial/application-debugging
    • 啊,我可以打开渲染器控制台。这是错误: Uncaught ReferenceError: require is not defined at renderer.js:8 这是什么以及为什么?
    猜你喜欢
    • 2020-04-03
    • 2020-09-02
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 2016-05-25
    • 2021-02-11
    • 2019-10-24
    • 2019-08-09
    相关资源
    最近更新 更多