Vize

script/prefer-use-template-ref

模板引用建议使用 useTemplateRef 而不是 ref(Vue 3.5+)。

默认严重程度 预设 自动修复 选项
warning opinionated, nuxt 不支持 无

适用范围:Vue SFC 中的 JS/TS 脚本;示例展示对应的 Options API 或 script setup 形式。

错误示例

可为空的 input ref 与模板字面量 ref="input" 配对,表明它是元素引用,而非普通可空数据。

<script setup lang="ts">
import { ref } from 'vue'
const input = ref<HTMLInputElement | null>(null)
</script>
<template>
  <input ref="input" />
</template>

正确示例

Vue 3.5+ 的 useTemplateRef<HTMLInputElement>('input') 显式表达模板引用。未配对的 error = ref(null) 仍是普通数据,有意不在此规则范围内。

<script setup lang="ts">
import { ref, useTemplateRef } from 'vue'
// Paired with the template ref below.
const input = useTemplateRef<HTMLInputElement>('input')
// A nullable data ref the template never binds as a ref.
const error = ref(null)
</script>
<template>
  <input ref="input" />
  <p>{{ error }}</p>
</template>

配置(Vite+)

import { defineConfig } from "@vizejs/vite-plugin/vite-plus";

export default defineConfig({}).withVue({
  linter: {
    "preset": "incremental",
    "rules": {
      "script/prefer-use-template-ref": "warn"
    }
  },
});

然后运行 vp run lint。

相关页面