noVueDeprecatedEventsApi (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noVueDeprecatedEventsApi - This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
- Inspired from
vue/no-deprecated-events-api
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noVueDeprecatedEventsApi": "error" } } }}Description
Section titled “Description”Disallow the Vue $on, $off, and $once instance methods.
Vue 3 removed the $on, $off, and $once methods from component instances, so a component can no longer
listen to its own events with them. Calling one of these methods throws an error, because the method doesn’t exist.
To react to an event that a child component emits, listen to it with v-on (or @) in the parent’s template.
To send events between components that aren’t parent and child, use a small event library such as
mitt or tiny-emitter.
This rule checks calls on this, and on variables declared with const vm = this, inside the options object
of a component: the default export of a .vue file, or the object passed to defineComponent() or createApp().
Calls written as this.$on?.() are allowed, because they only run when the method exists.
See the Vue 3 migration guide for more information.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<script>export default { mounted() { this.$on("start", () => { console.log("start"); }); },};</script>code-block.vue:4:10 lint/nursery/noVueDeprecatedEventsApi ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The Vue $on instance method was removed in Vue 3.
2 │ export default {
3 │ mounted() {
> 4 │ this.$on("start", () => {
│ ^^^
5 │ console.log("start");
6 │ });
ℹ Component instances no longer have this method, so calling it throws an error.
ℹ Listen to a child component's events with v-on in the parent's template, or use an event library such as mitt. See the Vue 3 migration guide for more information.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import { defineComponent } from "vue";
export default defineComponent({ beforeUnmount() { this.$off("start"); },});code-block.js:5:10 lint/nursery/noVueDeprecatedEventsApi ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The Vue $off instance method was removed in Vue 3.
3 │ export default defineComponent({
4 │ beforeUnmount() {
> 5 │ this.$off("start");
│ ^^^^
6 │ },
7 │ });
ℹ Component instances no longer have this method, so calling it throws an error.
ℹ Listen to a child component's events with v-on in the parent's template, or use an event library such as mitt. See the Vue 3 migration guide for more information.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
<script>import mitt from "mitt";
const emitter = mitt();
export default { mounted() { emitter.on("start", () => { console.log("start"); }); this.$emit("ready"); },};</script>Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.