Vue3 Fundamentals · Rendering & Templates

Attribute bindings

Attribute binding with v-bind

Attribute bindings

You want a link's destination to come from your data. Text interpolation will not help: inside an attribute, {{ }} is read as ordinary characters, so the browser gets a literal {{ url }} instead of the address.

Vue binds an attribute with the v-bind directive. Put v-bind: before the attribute name and give it an expression:

<a v-bind:href="url">Vue</a>

The v-bind: prefix is so common that it has a shorthand — a single colon:

<a :href="url">Vue</a>

Both forms mean the same thing: the attribute's value is the result of the expression, re-evaluated whenever the data changes. You can bind any attribute this way, and the shorthand is what you will see in real Vue code.

A bound attribute is not a string literal: :href="url" passes the value of url, while href="url" would pass the four letters.

Your job is to bind a link's href to a value instead of hard-coding it.

Challenge

The link always points at the home page, no matter what url holds. Export an App component from App.vue that binds the anchor's href to the url value, so the link points at https://vuejs.org.

The Test mounts your component and reads the anchor's href attribute. The starter hard-codes /, so the Test fails — bind the attribute with v-bind (or its : shorthand).

App.vue
<script setup>
const url = 'https://vuejs.org';
</script>

<template>
  <a href="/">Vue</a>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.