Performance: Dynamic attribute names
This affects how Vue updates the DOM attributes dynamically, impacting rendering speed and layout stability.
<template> <input :placeholder="dynamicAttrName === 'placeholder' ? value : ''" :title="dynamicAttrName === 'title' ? value : ''" /> </template> <script setup> import { ref } from 'vue' const dynamicAttrName = ref('placeholder') const value = ref('Enter text') </script>
<template> <input v-bind:[dynamicAttrName]="value" /> </template> <script setup> import { ref } from 'vue' const dynamicAttrName = ref('placeholder') const value = ref('Enter text') </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Dynamic attribute names with v-bind:[attr] | Multiple attribute updates | Triggers 1 reflow per update | Moderate paint cost | [X] Bad |
| Conditional static attribute bindings | Minimal attribute updates | Single reflow on value change | Lower paint cost | [OK] Good |
v-bind:[attrName] do in Vue?v-bind usagev-bind is used to bind attributes dynamically in Vue templates.v-bind:[attrName] means the attribute name is taken from the variable attrName, not a fixed string.attrName dynamically. -> Option Dv-bind:[attrName] binds attribute literally named 'attrName'v-bind without brackets for dynamic namesattr in Vue?v-bind or shorthand :.<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.<template> <button v-bind:[dynamicAttr]="value">Click</button> </template> <script setup> const dynamicAttr = 'aria-label' const value = 'Submit Button' </script>
dynamicAttr holds the string 'aria-label', so the attribute name will be 'aria-label'.<div v-bind:dynamicAttr="value">Hello</div> <script setup> const dynamicAttr = 'title' const value = 'Greeting' </script>
v-bind:dynamicAttr binds an attribute literally named 'dynamicAttr'.v-bind:[dynamicAttr].attrName and attrValue, then binds the attribute named by attrName with attrValue on a <div>. Which code correctly implements this?: with brackets around the prop name: :[attrName].<template> <div v-bind:[attrName]="attrValue">Content</div> </template> <script setup> const props = defineProps(['attrName', 'attrValue']) </script>correctly uses quotes around
attrValue. <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.
:[attrName]="attrValue" for dynamic binding [OK]Pass the test and this pattern moves unknown