Skip to content

noVueLifecycleAfterAwait (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noVueLifecycleAfterAwait": "error"
}
}
}
}

Disallow registering Vue lifecycle hooks after an await in setup().

Lifecycle hooks, such as onMounted() and onUnmounted(), attach code to the component whose setup() function is currently running. When setup() pauses at an await, Vue stops tracking which component is being set up. A lifecycle hook called after that point isn’t attached to the component, so its code never runs.

This rule reports calls to these functions imported from vue that come after an await in a component’s setup() function: onActivated, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onUnmounted, and onUpdated.

A call that passes the component as the second argument is allowed, because it doesn’t depend on the component that is currently being set up.

Code inside <script setup> isn’t checked, because Vue keeps track of the component across each await there.

<script>
import { onMounted } from "vue";
export default {
async setup() {
await fetchUser();
onMounted(() => {});
},
};
</script>
code-block.vue:7:5 lint/nursery/noVueLifecycleAfterAwait ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This lifecycle hook is registered after an await, so it never runs.

5 │ async setup() {
6 │ await fetchUser();
> 7 │ onMounted(() => {});
│ ^^^^^^^^^^^^^^^^^^^
8 │ },
9 │ };

ℹ The setup() function pauses here. After this point, Vue no longer knows which component the hook belongs to.

4 │ export default {
5 │ async setup() {
> 6 │ await fetchUser();
│ ^^^^^
7 │ onMounted(() => {});
8 │ },

ℹ Call the lifecycle hook before the first await in setup().

ℹ 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 { onMounted } from "vue";
export default {
async setup() {
onMounted(() => {});
await fetchUser();
},
};
</script>