script/no-lifecycle-after-await
Disallow lifecycle hooks registered after an await in setup().
| Default severity | Presets | Automatic fix | Options |
|---|---|---|---|
error |
essential, happy-path, ecosystem, nuxt, opinionated |
No | None |
Applies to: JS/TS scripts in Vue SFCs; examples show the relevant Options API or script setup form.
Bad
setup() calls onMounted() after await loadData(); with no current component instance at that point, the hook is never registered.
<script lang="ts">
import { onMounted } from 'vue'
export default {
async setup() {
await loadData()
onMounted(() => {})
},
}
</script>
Good
onMounted() is registered before the first await, while setup() still runs synchronously for the component.
<script lang="ts">
import { onMounted } from 'vue'
export default {
async setup() {
onMounted(() => {})
await loadData()
},
}
</script>
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"script/no-lifecycle-after-await": "error"
}
},
});
Then run vp run lint.