Attribute binding with v-bind lets you connect HTML attributes to Vue data. This means the attribute updates automatically when your data changes.
Attribute binding with v-bind in Vue
<element v-bind:attribute="dataProperty"></element>You can shorten v-bind:attribute to just :attribute.
The value inside quotes is a JavaScript expression evaluated in Vue's data context.
src attribute of the image to the imageUrl data property.<img v-bind:src="imageUrl" alt="A picture">
href attribute to linkUrl.<a :href="linkUrl">Go to site</a>isDisabled is true.<button :disabled="isDisabled">Click me</button>This Vue component lets you type an image URL in the input box. The img tag's src attribute updates automatically using v-bind (shortened as :). You see the image preview change as you type.
It uses ref to create reactive data and v-model to bind the input value.
<template>
<div>
<input v-model="imageUrl" placeholder="Enter image URL" aria-label="Image URL input" />
<p>Image preview:</p>
<img :src="imageUrl" alt="User provided image" style="max-width: 300px;" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const imageUrl = ref('https://vuejs.org/images/logo.png')
</script>Always use quotes around the JavaScript expression in v-bind.
Use v-bind to bind any attribute, including src, href, alt, disabled, and more.
Remember to provide meaningful alt text for images for accessibility.
v-bind connects HTML attributes to Vue data so they update automatically.
You can shorten v-bind:attr to :attr for cleaner code.
Use attribute binding to make your UI dynamic and responsive to data changes.