Skip to content
← Back to rules

vue/no-deprecated-events-api 正确性

它的作用

禁止在 Vue.js 3.0.0+ 中使用已弃用的 Events API($on$off$once)。

为什么这不好?

在 Vue.js 3.0.0+ 中,内部事件 API $on$off$once 已被移除。 这些方法曾用于组件之间的事件处理,但现在已不再可用。

示例

此规则的错误代码示例:

vue
<script>
export default {
  mounted() {
    this.$on("event", () => {});
    this.$off("event");
    this.$once("event", () => {});
  },
};
</script>

此规则的正确代码示例:

vue
<script>
import mitt from "mitt";

const emitter = mitt();

export default {
  mounted() {
    emitter.on("event", () => {});
    emitter.off("event");
    emitter.once("event", () => {});
  },
};
</script>

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["vue"],
  "rules": {
    "vue/no-deprecated-events-api": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vue"],
  rules: {
    "vue/no-deprecated-events-api": "error",
  },
});
bash
oxlint --deny vue/no-deprecated-events-api --vue-plugin

版本

此规则于 v1.62.0 中添加。

参考资料