script/no-arrow-functions-in-watch
禁止将箭头函数用作 Options API watch 处理器。
| 默认严重程度 | 预设 | 自动修复 | 选项 |
|---|---|---|---|
error |
essential, happy-path, ecosystem, nuxt, opinionated |
不支持 | 无 |
适用范围:Vue SFC 中的 JS/TS 脚本;示例展示对应的 Options API 或 script setup 形式。
错误示例
Options API 侦听器 value 和嵌套的 other.handler 都是箭头函数。箭头函数捕获外层 this,不会接收组件实例。
<script lang="ts">
export default {
watch: {
// `this` is not the component instance inside an arrow function.
value: () => {
this.doSomething()
},
other: {
handler: () => {}
}
}
}
</script>
正确示例
两个处理器改为普通方法,使 Vue 能将 this 绑定到组件。deep: true 侦听选项仍与对象形式兼容。
<script lang="ts">
export default {
watch: {
value(newValue, oldValue) {
this.doSomething()
},
other: {
handler(newValue) {},
deep: true
}
}
}
</script>
配置(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"script/no-arrow-functions-in-watch": "error"
}
},
});
然后运行 vp run lint。