Dynamic attribute names let you set HTML attributes using variable names. This helps when you want to change attribute names based on data or conditions.
Dynamic attribute names in Vue
<element v-bind:[attributeName]="value"></element>The square brackets [] around attributeName tell Vue to treat it as a variable.
The value can be any JavaScript expression or variable.
attrName. It will set that attribute with the value of buttonText.<button v-bind:[attrName]="buttonText">Click me</button>dynamicAttr and sets its value to inputValue.<input v-bind:[dynamicAttr]="inputValue" />data-type.<div v-bind:[`data-${type}`]="info"></div>
This Vue component binds the title attribute dynamically to the input element. The attribute name is stored in attrName. The input's title attribute will show a tooltip with 'Hello Vue!' when hovered.
<template>
<div>
<input v-bind:[attrName]="inputValue" placeholder="Type something" />
<p>Attribute name: {{ attrName }}</p>
<p>Input value: {{ inputValue }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const attrName = ref('title')
const inputValue = ref('Hello Vue!')
</script>Dynamic attribute names only work with v-bind and require square brackets.
Make sure the attribute name is a valid HTML attribute or custom attribute.
Use this feature to keep your templates flexible and reusable.
Dynamic attribute names let you set attribute names using variables.
Use v-bind:[variable] syntax to bind dynamically.
This helps make components flexible and adapt to different needs.
Practice
v-bind:[attrName] do in Vue?Solution
Step 1: Understand
v-bindusagev-bindis used to bind attributes dynamically in Vue templates.Step 2: Analyze dynamic attribute syntax
Using square brackets likev-bind:[attrName]means the attribute name is taken from the variableattrName, not a fixed string.Final Answer:
Binds an attribute with a name stored in the variableattrNamedynamically. -> Option DQuick Check:
Dynamic attribute name = variable inside brackets [OK]
- Thinking
v-bind:[attrName]binds attribute literally named 'attrName' - Confusing dynamic binding with static attribute binding
- Assuming it creates a new component
- Using
v-bindwithout brackets for dynamic names
attr in Vue?Solution
Step 1: Recall dynamic attribute binding syntax
Dynamic attribute names require square brackets around the variable name insidev-bindor shorthand:.Step 2: Check each option
<div v-bind:attr>binds a static attribute named 'attr'.<div v-bind:[attr]>correctly usesv-bind:[attr].<div :attr>binds a static attribute named 'attr'.<div :[attr]>is invalid syntax.Final Answer:
<div v-bind:[attr]> -> Option AQuick Check:
Use brackets for dynamic attribute names [OK]
- Omitting brackets for dynamic attribute names
- Using shorthand without brackets incorrectly
- Confusing static and dynamic attribute binding
<template> <button v-bind:[dynamicAttr]="value">Click</button> </template> <script setup> const dynamicAttr = 'aria-label' const value = 'Submit Button' </script>
What attribute will the rendered button have?
Solution
Step 1: Identify the dynamic attribute name
The variabledynamicAttrholds the string 'aria-label', so the attribute name will be 'aria-label'.Step 2: Understand the binding value
The value bound to this attribute is the string 'Submit Button'.Final Answer:
aria-label="Submit Button" -> Option CQuick Check:
Variable value used as attribute name [OK]
- Using variable name as attribute name literally
- Confusing attribute name with value
- Expecting no attribute added
<div v-bind:dynamicAttr="value">Hello</div> <script setup> const dynamicAttr = 'title' const value = 'Greeting' </script>
Solution
Step 1: Check the binding syntax
Without brackets,v-bind:dynamicAttrbinds an attribute literally named 'dynamicAttr'.Step 2: Understand dynamic attribute binding
To use the variable's value as attribute name, brackets are required:v-bind:[dynamicAttr].Final Answer:
It will bind an attribute literally named 'dynamicAttr', not 'title'. -> Option BQuick Check:
Missing brackets means static attribute name [OK]
- Forgetting brackets for dynamic attribute names
- Assuming variable name is used as attribute name
- Thinking this causes syntax error
attrName and attrValue, then binds the attribute named by attrName with attrValue on a <div>. Which code correctly implements this?Solution
Step 1: Understand dynamic attribute binding with props
To bind an attribute dynamically using a prop, use the shorthand:with brackets around the prop name::[attrName].Step 2: Check each option's syntax
<template> <div v-bind:[attrName]="attrValue">Content</div> </template> <script setup> const props = defineProps(['attrName', 'attrValue']) </script>
correctly uses quotes aroundattrValue.<template> <div v-bind:attrName="attrValue">Content</div> </template> <script setup> const props = defineProps(['attrName', 'attrValue']) </script>
and C bind static attribute 'attrName'.<template> <div :attrName="attrValue">Content</div> </template> <script setup> const props = defineProps(['attrName', 'attrValue']) </script>
and<template> <div :[attrName]="attrValue">Content</div> </template> <script setup> const props = defineProps(['attrName', 'attrValue']) </script>
correctly use:[attrName]="attrValue"with props defined, but<template> <div :[attrName]="attrValue">Content</div> </template> <script setup> const props = defineProps(['attrName', 'attrValue']) </script>
uses the shorthand syntax preferred in Vue.Final Answer:
<div :[attrName]="attrValue">Content</div> -> Option AQuick Check:
Use:[attrName]="attrValue"for dynamic binding [OK]
- Omitting quotes around binding value
- Using static attribute name instead of dynamic
- Confusing shorthand and full syntax
Ready to prove it?
Pass the test and this pattern moves unknown
