index.js中监听事件没反应

来源:4-2 创建添加音乐窗口

alex__wyh

2019-06-12

  1. 我在html文件中给添加按钮设置一个id
  2. 然后在js文件中给这个按钮绑定一个事件
  3. 然后当我点击按钮时,这个事件没有被触发(控制台没输出消息),下面是代码
    index.html的代码
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>本地播放器</title>
    <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
  </head>
  <body>
    <div class="container mt-4">
      <h1>我的播放器</h1>
      <button type="button" id="add-music-button" class="btn btn-primary btn-lg btn-block mt-4">
        添加歌曲到曲库
      </button>
    </div>
    <script>
      require('./index.js')
    </script>
  </body>
</html>

index.js的代码

const {ipcRenderer} = require('electron')

document.getElementById('add-music-button').addEventListener('click', () => {
	console.log('hello from index')
	ipcRenderer.send('add-music-window')
})
写回答

3回答

张轩

2019-06-13

同学你好,你说的控制台是浏览器的控制台?还是你的 terminal?

0
1
alex__wyh
为什么主进程的输出是在终端中,渲染进程中的输出是在调试工具中
2019-06-13
共1条回复

yuzhewo

2019-06-13

打开devtools看下,渲染进程中的输出是在调试工具中输出的 hello from index

0
1
alex__wyh
嗯嗯,我后来发现了
2019-06-13
共1条回复

yuzhewo

2019-06-13

遇到同样的问题,注意文件保存,然后重新启动程序有时候问题就会解决,不一定是代码的问题。

0
0

Electron开发仿网易云音乐播放器

站内首门Electron课程,让你更专注应用的核心开发

3428 学习 · 159 问题

查看课程