v-html — Rendering Raw HTML
Shelf's team writes a short "Book of the week" blurb in the admin panel, stored as HTML with bold titles and links. Printed with double braces, the reader sees raw tags like <strong>Dune</strong>. To have the browser treat the string as markup, you need v-html, and you need to know exactly when it is safe.
The rule
v-html trusts the string completely, so feed it only trusted HTML.
<script setup>
const blurb = '<strong>Dune</strong> is this week\'s pick. <a href="/books/1">Read more</a>'
</script>
<template>
<p>{{ blurb }}</p> <!-- shows the tags as text -->
<div v-html="blurb"></div> <!-- shows bold text and a link -->
</template>What v-html does, piece by piece:
- Replaces the content. Whatever was inside the
divis thrown away and the string's HTML takes its place. Do not put children inside an element that hasv-html. - Plain HTML only. The string is not a Vue template. If it contains
{{ title }}or<BookCard />, they are not processed; they just appear as text or an unknown tag. - Scoped styles do not reach it. Scoped CSS works by stamping elements Vue creates from the template. Elements that arrive through
v-htmlare not stamped, so scoped rules skip them. Style them with global CSS or the:deep()selector. - No escaping. This is the dangerous part. If the string came from a user, it can carry markup such as an image with an
onerrorattribute that runs JavaScript in every reader's browser. That is a cross-site scripting (XSS) attack.