vue/use-unique-element-ids
Enforce unique element IDs using useId() instead of static literals
Default severity: warning
Presets: nuxt, opinionated
Automatic fix: None; review the suggested change
Applies to: Vue SFC templates and blocks, with script context where the rule requires it
Options: No rule-specific options. Severity and preset selection are configurable.
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({
lint: {
vize: {
"preset": "incremental",
"rules": {
"vue/use-unique-element-ids": "warn"
}
},
},
});
vp run lint
Bad
The literal email ID is reused by every instance of this component, which can misdirect its label when several instances are rendered.
<template>
<label for="email">Email</label>
<input id="email" />
</template>
Good
useId() produces the instance's emailId; bind the same value to the label's for and the input's id.
<script setup>
import { useId } from "vue";
const emailId = useId();
</script>
<template>
<label :for="emailId">Email</label>
<input :id="emailId" />
</template>
Good avoids this rule's finding under the configuration above; other rules may still report diagnostics.