简体   繁体   English

ESLint 禁止使用警报(无警报)

[英]ESLint Disallow Use of Alert (no-alert)

how can i turn the following alert into an acceptable alert for ESLint?如何将以下警报转换为 ESLint 可接受的警报?

svg.onerror = function() {
    alert("File cannot be loaded");
};

My build fails because apparently i cant use "alert".我的构建失败了,因为显然我不能使用“警报”。 I want to call this alert when there is an issue loading something.我想在加载某些内容时出现问题时调用此警报。 This code works successfully but it does not comply with ESLint.此代码成功运行,但不符合 ESLint。

http://eslint.org/docs/rules/no-alert http://eslint.org/docs/rules/no-alert

how can i modify my code to make it build successfully?如何修改我的代码以使其成功构建?

thanks in advance :)提前致谢 :)

If you think you have a good reason for not observing an eslint setting, then you can disable that specific rule for that specific line by specifying eslint-disable-line in a comment on the offending line, like so:如果您认为有充分的理由不遵守 eslint 设置,那么您可以通过在违规行的注释中指定eslint-disable-lineeslint-disable-line该特定行的特定规则,如下所示:

svg.onerror = function() {
    alert("File cannot be loaded");  // eslint-disable-line no-alert
};

A better solution might be to implement a modal or show an error to the user in another less-intrusive way (eg: toggle visibility of an error element, or use something like Bootstrap's modal).更好的解决方案可能是实现一个模态或以另一种较少侵入的方式向用户显示错误(例如:切换错误元素的可见性,或使用类似 Bootstrap 的模态)。

Some ESLint rules are don't make sense to include in every project, and no-alert is definitely one of them.一些 ESLint 规则并不适合包含在每个项目中, no-alert绝对是其中之一。 While critics might say alert is obtrusive, that fails to account for the fact that obtrusive can be good!虽然批评者可能会说alert是突兀的,但这并不能解释突兀可能是好的事实

To disable this rule, disable it in your ESLint configuration file by setting it to "off."要禁用此规则,请在 ESLint 配置文件中将其设置为“off”以禁用它。

...
"rules": {
    ...
    "no-alert": "off",
    ...
}

ESlint: Turning off a specific rule across a project ESlint:关闭项目中的特定规则

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM