Sometimes you want to show HTML code directly inside your Vue app. The v-html directive lets you do that by inserting raw HTML into your page.
Raw HTML with v-html in Vue
<element v-html="rawHtmlContent"></element>The value inside v-html must be a string containing valid HTML.
Vue will replace the element's inner content with this HTML.
message variable inside the div.<div v-html="message"></div>v-html to render formatted text.<p v-html="'<strong>Bold text</strong> and <em>italic text</em>'"></p>This Vue component shows how to use v-html to display raw HTML stored in a reactive variable. The HTML tags inside the string are rendered as formatted text.
<template>
<div>
<h2>Raw HTML Example</h2>
<div v-html="rawHtml"></div>
</div>
</template>
<script setup>
import { ref } from 'vue'
const rawHtml = ref('<p>This is <strong>bold</strong> and this is <em>italic</em>.</p>')
</script>Be careful with v-html because it can open security risks if you insert untrusted HTML (like user input) without cleaning it first.
Use v-html only when you trust the HTML content or sanitize it properly.
v-html lets you insert raw HTML inside your Vue components.
It replaces the element's inner content with the HTML string you provide.
Always be cautious about security when using v-html.
Practice
v-html directive do in Vue?Solution
Step 1: Understand the purpose of
Thev-htmlv-htmldirective is used to insert raw HTML content inside an element in Vue.Step 2: Compare with other options
Other options describe different Vue features like event listening or styling, which are unrelated tov-html.Final Answer:
It inserts raw HTML content inside an element. -> Option CQuick Check:
v-html = inserts raw HTML [OK]
- Confusing v-html with event binding
- Thinking v-html applies styles
- Assuming v-html binds JavaScript objects
v-html in a Vue template?Solution
Step 1: Identify correct directive syntax
The correct directive to insert raw HTML isv-html, used asv-html="rawHtml".Step 2: Check other options for correctness
uses a non-existent directive:html. usesv-textwhich inserts plain text, not HTML. uses an invalid directivev-bind-html.Final Answer:
<div v-html="rawHtml"></div> -> Option BQuick Check:
Correct syntax = v-html="variable" [OK]
- Using v-text instead of v-html
- Trying v-bind-html which is invalid
- Using shorthand :html which doesn't exist
div?
<template> <div v-html="htmlContent"></div> </template> <script setup> const htmlContent = '<strong>Hello</strong> <em>Vue</em>!' </script>
Solution
Step 1: Understand v-html rendering
Thev-htmldirective inserts the HTML string as actual HTML, so tags like<strong>and<em>become formatted text.Step 2: Analyze the given HTML string
The string contains<strong>Hello</strong>and<em>Vue</em>, so the output will show "Hello" in bold and "Vue" in italic.Final Answer:
<strong>Hello</strong> <em>Vue</em>! (formatted with bold and italic) -> Option AQuick Check:
v-html renders HTML tags as formatted content [OK]
- Expecting raw HTML tags to show as text
- Confusing v-html with v-text
- Assuming an error occurs with HTML tags
<template> <div v-html=htmlContent></div> </template> <script setup> const htmlContent = '<p>Hello</p>' </script>
Solution
Step 1: Check v-html syntax
The directive value must be quoted, likev-html="htmlContent". Here it is missing quotes.Step 2: Verify other options
The variable is valid and reactive by default. The HTML string is valid. v-html works on any element includingdiv.Final Answer:
The v-html directive is missing quotes around the binding expression. -> Option AQuick Check:
v-html value must be quoted [OK]
- Omitting quotes around v-html value
- Thinking v-html only works on certain tags
- Assuming HTML string causes errors
v-html. Which approach helps prevent security risks?Solution
Step 1: Understand security risks of v-html
Usingv-htmlwith user input can cause cross-site scripting (XSS) attacks if the HTML is not safe.Step 2: Identify safe usage
Sanitizing the HTML string removes dangerous code before inserting it, making it safe to use withv-html.Step 3: Evaluate other options
Using v-html directly is unsafe. Escaping tags then using v-html defeats the purpose. Using v-text shows raw text, not HTML.Final Answer:
Sanitize the HTML string before binding it to v-html. -> Option DQuick Check:
Sanitize user HTML before v-html to prevent XSS [OK]
- Using v-html directly with user input
- Escaping HTML then using v-html (no effect)
- Confusing v-text with v-html for HTML rendering
Ready to prove it?
Pass the test and this pattern moves unknown
