script/no-arrow-functions-in-watch
Disallow arrow functions as Options API watch handlers
Default severity: error
Presets: essential, happy-path, ecosystem, nuxt, opinionated
Automatic fix: None; review the suggested change
Applies to: JS/TS scripts in Vue SFCs; examples show the relevant Options API or script setup form
Options: No rule-specific options. Severity and preset selection are configurable.
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({
lint: {
vize: {
"preset": "incremental",
"rules": {
"script/no-arrow-functions-in-watch": "error"
}
},
},
});
vp run lint
Bad
The Options API watcher value and the nested other.handler are arrow functions. An arrow captures its surrounding this rather than receiving the component instance.
<script lang="ts">
export default {
watch: {
// `this` is not the component instance inside an arrow function.
value: () => {
this.doSomething()
},
other: {
handler: () => {}
}
}
}
</script>
Good
Both handlers become ordinary methods, allowing Vue to bind this to the component. The deep: true watcher option remains compatible with the object form.
<script lang="ts">
export default {
watch: {
value(newValue, oldValue) {
this.doSomething()
},
other: {
handler(newValue) {},
deep: true
}
}
}
</script>
Good avoids this rule's finding under the configuration above; other rules may still report diagnostics.