📖 Scenario: You are building a simple Vue 3 component that displays a button. The button's attribute name changes dynamically based on a variable. This is useful when you want to toggle attributes like disabled, aria-pressed, or others depending on the app state.
🎯 Goal: Create a Vue 3 component that uses a dynamic attribute name bound to a variable. The attribute should be set to true on the button element.
📋 What You'll Learn
Create a reactive variable called attrName with the value 'aria-pressed'.
Create a reactive variable called attrValue with the value true.
Use Vue's : [ ] binding syntax to bind the attribute named by attrName to the value in attrValue on a <button> element.
The button should display the text Toggle.
💡 Why This Matters
🌍 Real World
Dynamic attribute binding is useful in real apps to toggle accessibility attributes or states like 'disabled', 'aria-pressed', or custom data attributes based on user interaction or app state.
💼 Career
Understanding dynamic bindings in Vue is important for frontend developers to build flexible, accessible, and interactive user interfaces.
Progress0 / 4 steps
1
Setup Vue component with reactive attribute name
Create a Vue 3 component using <script setup>. Inside it, create a reactive variable called attrName and set it to the string 'aria-pressed'.
Vue
Hint
Use ref from Vue to create a reactive variable named attrName.
2
Add reactive attribute value
Inside the same <script setup>, create another reactive variable called attrValue and set it to the boolean true.
Vue
Hint
Use ref again to create attrValue with the boolean true.
3
Bind dynamic attribute to button
In the <template>, add a <button> element. Use Vue's dynamic binding syntax with square brackets to bind the attribute named by attrName to the value in attrValue. The button text should be Toggle.
Vue
Hint
Use v-bind:[attrName]="attrValue" to bind the dynamic attribute on the button.
4
Complete component with accessibility and responsiveness
Add a type attribute with value button to the <button> element for accessibility. Also, add a simple style block below the template that sets the button's font size to 1.2rem and padding to 0.5rem 1rem.
Vue
Hint
Remember to add type="button" for accessibility and style the button with CSS inside a <style> block.
Practice
(1/5)
1. What does v-bind:[attrName] do in Vue?
easy
A. Creates a new component named attrName.
B. Binds the attribute literally named 'attrName' to the element.
C. Binds a static attribute called 'v-bind'.
D. Binds an attribute with a name stored in the variable attrName dynamically.
Solution
Step 1: Understand v-bind usage
v-bind is used to bind attributes dynamically in Vue templates.
Step 2: Analyze dynamic attribute syntax
Using square brackets like v-bind:[attrName] means the attribute name is taken from the variable attrName, not a fixed string.
Final Answer:
Binds an attribute with a name stored in the variable attrName dynamically. -> Option D
Quick Check:
Dynamic attribute name = variable inside brackets [OK]
Hint: Square brackets mean attribute name is from a variable [OK]
Common Mistakes:
Thinking v-bind:[attrName] binds attribute literally named 'attrName'
Confusing dynamic binding with static attribute binding
Assuming it creates a new component
Using v-bind without brackets for dynamic names
2. Which of the following is the correct syntax to bind a dynamic attribute named by the variable attr in Vue?
easy
A. <div v-bind:[attr]>
B. <div v-bind:attr>
C. <div :attr>
D. <div :[attr]>
Solution
Step 1: Recall dynamic attribute binding syntax
Dynamic attribute names require square brackets around the variable name inside v-bind or shorthand :.
Step 2: Check each option
<div v-bind:attr> binds a static attribute named 'attr'. <div v-bind:[attr]> correctly uses v-bind:[attr]. <div :attr> binds a static attribute named 'attr'. <div :[attr]> is invalid syntax.
Final Answer:
<div v-bind:[attr]> -> Option A
Quick Check:
Use brackets for dynamic attribute names [OK]
Hint: Use brackets around variable for dynamic attribute [OK]
A. It will cause a syntax error because v-bind needs brackets.
B. It will bind an attribute literally named 'dynamicAttr', not 'title'.
C. It will bind the attribute 'value' instead of 'title'.
D. There is no error; it works as expected.
Solution
Step 1: Check the binding syntax
Without brackets, v-bind:dynamicAttr binds 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 B
Quick Check:
Missing brackets means static attribute name [OK]
Hint: Use brackets for dynamic attribute names, else static name used [OK]
Common Mistakes:
Forgetting brackets for dynamic attribute names
Assuming variable name is used as attribute name
Thinking this causes syntax error
5. You want to create a Vue component that accepts a prop attrName and attrValue, then binds the attribute named by attrName with attrValue on a <div>. Which code correctly implements this?