Skip to content

noVueUnusedRefs (HTML)

Language HTML
biome.json
{
"linter": {
"rules": {
"nursery": {
"noVueUnusedRefs": "error"
}
}
}
}

Disallow template refs that are never used.

A ref attribute gives a name to an element of the template, so that the component’s code can reach that element through $refs or useTemplateRef(). When nothing reads that name, the attribute has no effect. It is usually left behind after the code that used it was removed.

A template ref counts as used when:

  • the template or the script reads it from $refs, as in $refs.input or this.$refs.input;
  • the script passes its name to useTemplateRef(), as in useTemplateRef("input");
  • the component exposes a variable with the same name from <script setup> or from the object that setup() returns. Vue stores the element in that variable.

Only refs written as plain text, such as ref="input", are checked. A ref bound with :ref is never reported.

The rule reports nothing when it can’t know which refs are used. That is the case when:

  • a ref is read with a name that is not written as a string, as in $refs[name] or useTemplateRef(name);
  • $refs itself is stored or handed to other code, as in const refs = this.$refs;
  • a <script src> block loads part of the component from another file.

Refs read from another component, for example through $parent.$refs, are not detected.

<template>
<input ref="input" />
</template>
<script>
export default {
mounted() {
this.$refs.input.focus();
},
};
</script>
<template>
<input ref="input" />
<button ref="submit">Submit</button>
</template>
<script setup>
import { ref, useTemplateRef } from "vue";
const input = useTemplateRef("input");
const submit = ref(null);
</script>
<template>
<input ref="input" />
<button ref="submit">Submit</button>
<span ref="label" @click="$refs.label.focus()"></span>
</template>