vue/use-unique-element-ids
静的 ID の代わりに useId() で一意な ID を生成します。
| 既定の重大度 | プリセット | 自動修正 | オプション |
|---|---|---|---|
warning |
nuxt, opinionated |
なし | なし |
適用範囲:Vue SFC のテンプレートと各ブロック。ルールに必要な script の文脈も例に含めています。
悪い例
固定の email ID がコンポーネントの各インスタンスで重複し、複数表示時に label の参照先が曖昧になります。
<template>
<label for="email">Email</label>
<input id="email" />
</template>
良い例
useId() の emailId を label の for と input の id の両方にバインドします。
<script setup>
import { useId } from "vue";
const emailId = useId();
</script>
<template>
<label :for="emailId">Email</label>
<input :id="emailId" />
</template>
設定(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"vue/use-unique-element-ids": "warn"
}
},
});
設定したら vp run lint を実行します。