script/prefer-ref-over-reactive
建议使用 ref() 而不是 reactive() 管理状态。
| 默认严重程度 | 预设 | 自动修复 | 选项 |
|---|---|---|---|
warning |
无 | 不支持 | 无 |
适用范围:Vue SFC 中的 JS/TS 脚本;示例展示对应的 Options API 或 script setup 形式。
错误示例
状态通过 reactive 创建,不符合此主张型规则对 refs 的偏好。示例展示风格偏好,并非响应式对象本身无效。
<script setup lang="ts">
// reactive requires careful handling to avoid losing reactivity
const state = reactive({
count: 0,
name: 'foo'
})
</script>
正确示例
示例通过 ref 创建标量和对象状态;相关字段也可拆为独立 refs,满足偏好的状态构造形式。
<script setup lang="ts">
// ref is more explicit and safer
const count = ref(0)
const name = ref('foo')
// For objects, ref still works
const user = ref({ name: 'foo', age: 20 })
// Or use multiple refs for related data
const userName = ref('foo')
const userAge = ref(20)
</script>
配置(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"script/prefer-ref-over-reactive": "warn"
}
},
});
然后运行 vp run lint。