script/prefer-use-template-ref
テンプレート参照に Vue 3.5 の useTemplateRef() を使います。
既定の重大度: warning
プリセット: none
自動修正: なし。修正内容を確認してください
適用範囲: Vue SFC の JS / TS script。Options API または script setup の対象は例を参照してください。
オプション: ルール固有のオプションはありません。重大度とプリセットは設定できます。
設定(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({
lint: {
vize: {
"preset": "incremental",
"rules": {
"script/prefer-use-template-ref": "warn"
}
},
},
});
vp run lint
悪い
null で初期化した input の ref がテンプレートのリテラル ref="input" と対応し、通常の nullable なデータではなく要素参照であることが分かります。
<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>
良い例は上記の設定でこのルールの検出を避ける例です。他のルールでは検出される場合があります。