Skip to content
← Back to rules

eslint/no-alert 限制

An auto-fix is available for this rule.

它的作用

禁止使用 alert、confirm 和 prompt

为什么这是个问题?

JavaScript 的 alert、confirm 和 prompt 函数作为 UI 元素被认为过于侵入性,应由更合适的自定义 UI 实现替代。
此外,alert 常用于调试代码,部署到生产环境前应予以移除。

示例

此规则的错误代码示例:

js
alert("这里!");

confirm("确定吗?");

prompt("你叫什么名字?", "约翰·多");

此规则的正确代码示例:

js
customAlert("发生了一些事情!");

customConfirm("确定吗?");

customPrompt("你是谁?");

function foo() {
  var alert = myCustomLib.customAlert;
  alert();
}

如何使用

通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "no-alert": "error"
  }
}
bash
oxlint --deny no-alert

参考资料