Performance: Attribute binding with v-bind
This affects how efficiently Vue updates HTML attributes in the DOM during reactive data changes.
<template> <button :disabled="isDisabled" @click="toggle">Click me</button> </template> <script setup> import { ref } from 'vue'; const isDisabled = ref(false); function toggle() { isDisabled.value = !isDisabled.value; } </script>
<template> <button id="btn" :disabled="isDisabled" @click="toggle">Click me</button> </template> <script setup> import { ref } from 'vue'; const isDisabled = ref(false); function toggle() { const btn = document.getElementById('btn'); if (btn.hasAttribute('disabled')) { btn.removeAttribute('disabled'); } else { btn.setAttribute('disabled', ''); } } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual DOM attribute manipulation | Multiple direct DOM queries and updates | 2+ reflows per update | High paint cost due to layout thrashing | [X] Bad |
| Vue v-bind reactive attribute binding | Single reactive update with batched DOM patch | 1 reflow per update | Lower paint cost due to efficient updates | [OK] Good |
v-bind do in Vue?v-bindv-bind connects HTML attributes to Vue data, making the UI update when data changes.v-bind:href in Vue?v-bind:attr is :attr.v-bind:href, the shorthand is :href.<a :href="url">Link</a>
{ url: 'https://vuejs.org' }, what will be the rendered href attribute in the browser?:href="url" binds the href attribute to the value of url in data.url is 'https://vuejs.org', the href attribute becomes this string.<img v-bind:src='imageUrl></img>
v-bind:src is missing the closing single quote after imageUrl.<a> tag in Vue. Which is the correct way to do this using v-bind shorthand?v-bind is a colon before the attribute name, and the value must be in quotes if it's a JavaScript expression.:class="linkClass" and :href="linkUrl". <a v-bind:class="linkClass" href="linkUrl">Click</a> misses binding on href. Options C and D have incorrect quoting or missing quotes.Pass the test and this pattern moves unknown