The v-bind Directive
The v-bind directive links an HTML attribute to a value in Vue. A directive is a special attribute that adds behavior to an element.
Link an image source
The photo holds the file name cat.jpg.
<img :src="photo">Link a web address
The link holds a web address.
<a :href="link">Visit</a>Link the disabled state
isClosed is true, so the button is disabled.
<button :disabled="isClosed">Join</button>The full form
v-bind:src means the same as :src.
<img v-bind:src="photo">Example
<script setup>
import { ref } from 'vue'
const photo = ref('cat.jpg')
const caption = ref('A cat')
const link = ref('https://a1learner.com')
const isClosed = ref(true)
</script>
<template>
<div>
<img :src="photo" :alt="caption">
<p>{{ caption }}</p>
</div>
</template>
Key points
- Write v-bind:src to link the src attribute to a value.
- The short form is a colon, like :src.
- The linked value can be a ref, a plain value, or a short expression.
- Vue updates the attribute when the linked value changes.
- Use v-bind for any attribute that must change, like src, href, or disabled.
In this example, the script makes a photo file name and a caption. It also makes a web address and a closed flag. The template links the img src and alt to those values with :src and :alt. The template also shows the caption in a paragraph. The page first shows the cat picture and the words A cat. If the values change to dog.jpg and A dog, the page shows the dog picture.
Note
Static attributes need no v-bind, so plain src stays as it is.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does :src do?