noVueLifecycleAfterAwait (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/noVueLifecycleAfterAwait - This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
- Same as
vue/no-lifecycle-after-await
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noVueLifecycleAfterAwait": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<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>Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.