noVueUnusedRefs (HTML)
Language HTML
Summary
Section titled “Summary”- Rule available since:
v2.6.4 - Diagnostic Category:
lint/nursery/noVueUnusedRefs - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- This rule belongs to the following domains:
- Sources:
- Same as
vue/no-unused-refs
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noVueUnusedRefs": "error" } } }}Description
Section titled “Description”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.inputorthis.$refs.input; - the script passes its name to
useTemplateRef(), as inuseTemplateRef("input"); - the component exposes a variable with the same name from
<script setup>or from the object thatsetup()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]oruseTemplateRef(name); $refsitself is stored or handed to other code, as inconst 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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<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>Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.