简体   繁体   English

ESLint 警告事件已定义但从未使用过

[英]ESLint warning event is defined but never used

I'm getting an error with ESLint on this simple bit of code:我在这段简单的代码中遇到 ESLint 错误:

var trigger = document.getElementById("hello");
var audio = new Audio('audio/hello.mp3');

window.addEventListener('DOMContentLoaded', (event) => {
    trigger.addEventListener("click", function(event) {
        event.preventDefault();
        audio.play();
    }, false);
});

The error I'm getting is: 'event' is defined but never used.我得到的错误是: 'event' is defined but never used.

I've read that I can disable this error by adding a comment after the open brace where 'event' is used like this: // eslint-disable-line no-unused-vars .我读过我可以通过在左括号后添加注释来禁用此错误,其中“事件”是这样使用的: // eslint-disable-line no-unused-vars

But my question is, is anything actually wrong with this code and should it be fixed or improved on?但我的问题是,这段代码实际上有什么问题吗?应该修复或改进吗? Rather than covered up by a comment ignoring the warning?而不是被忽略警告的评论所掩盖? Or is there actually nothing wrong with this approach and it's just ESLint being pedantic?还是这种方法实际上没有任何问题,只是 ESLint 迂腐?

But my question is, is anything actually wrong with this code and should it be fixed or improved on?但我的问题是,这段代码实际上有什么问题吗?应该修复或改进吗?

You're never using the event parameter you define in the DOMContentLoaded event listener (you're only using the one defined in the click handler), so you should remove it:您永远不会使用您在DOMContentLoaded事件侦听器中定义的event参数(您只使用在click处理程序中定义的参数),因此您应该删除它:

window.addEventListener('DOMContentLoaded', () => {
// No `event` here −−−−−−−−−−−−−−−−−−−−−−−−−−^

Having said that, though, using DOMContentLoaded is unnecessary if you control the script tag that loads the code.话虽如此,但如果您控制加载代码的script标记,则无需使用DOMContentLoaded If you do, just use type="module" or defer to ensure the code doesn't run until the DOM is built.如果这样做,只需使用type="module"defer以确保代码在构建 DOM 之前不会运行。

Linters can be annoying, but it's usually a good idea to try and keep them happy. Linters 可能很烦人,但尝试让它们开心通常是个好主意。 Instead of disabling the warning, though, you can indicate that you are deliberately ignoring a parameter by beginning its name with an underscore.但是,您可以通过以下划线开头的参数名称来指示您故意忽略该参数,而不是禁用该警告。

var trigger = document.getElementById("hello");
var audio = new Audio('audio/hello.mp3');

window.addEventListener('DOMContentLoaded', (_event) => {
    trigger.addEventListener("click", function(event) {
        event.preventDefault();
        audio.play();
    }, false);
});

You can also use just an underscore for the parameter name, or leave it out completely.您也可以只为参数名称使用下划线,或者完全不使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 eslint:禁用警告-特定 function 的“已定义但从未使用”? - eslint: disable warning - `defined but never used` for specific function? [eslint] src\App.js 第 2:8 行中的 React js 错误警告:'person' 已定义但从未使用过 - React js error WARNING in [eslint] src\App.js Line 2:8: 'person' is defined but never used “'React' 在定义之前就被使用了。” eslint 警告 - “'React' was used before it was defined.” eslint warning 防止JSHint警告“functionName已定义但从未使用过” - Prevent JSHint warning that 'functionName is defined but never used' 变量已定义但从未使用警告 javascript - Variable is defined but never used warning javascript 如何修复 eslint 在反应中的“组件已定义但从未使用”? - How to fix "Component is defined but never used" for eslint in react? React中的无状态组件和“Props已定义但从未使用过”ESLint错误 - Stateless component in React and “Props is defined but never used” ESLint error Eslint with Vue 给出 function 已定义但从未使用过 no-unused-vars - Eslint with Vue gives function is defined but never used no-unused-vars @typescript-eslint/eslint-plugin 错误:定义了“路由”但从未使用过(no-unused-vars) - @typescript-eslint/eslint-plugin error: 'Route' is defined but never used (no-unused-vars) 组件已定义但从未使用 reactjs 中的 no-unused-vars 错误警告 - component is defined but never used no-unused-vars error warning in reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM