0
0
Vueframework~10 mins

Raw HTML with v-html in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Raw HTML with v-html
Vue Component Render
Template with v-html
Vue Inserts Raw HTML
Browser Parses HTML
HTML Rendered in DOM
Vue reads the v-html directive, inserts raw HTML string into the DOM, and the browser renders it as HTML elements.
Execution Sample
Vue
<template>
  <div v-html="rawHtml"></div>
</template>
<script setup>
const rawHtml = '<p><strong>Hello</strong> <em>Vue!</em></p>'
</script>
This code inserts the raw HTML string into the div, rendering formatted text inside.
Execution Table
StepActionv-html ValueDOM ChangeRendered Output
1Vue reads template'<p><strong>Hello</strong> <em>Vue!</em></p>'No change yetEmpty div
2v-html directive inserts HTML string'<p><strong>Hello</strong> <em>Vue!</em></p>'div.innerHTML set to rawHtmldiv contains <p><strong>Hello</strong> <em>Vue!</em></p>
3Browser parses inserted HTMLN/ACreates <p>, <strong>, <em> elements inside divVisible formatted text: Hello Vue!
4Render completeN/ADOM stable with inserted HTMLUser sees bold 'Hello' and italic 'Vue!' inside the div
💡 Raw HTML inserted and parsed, rendering complete.
Variable Tracker
VariableStartAfter Step 2Final
rawHtmlundefined'<p><strong>Hello</strong> <em>Vue!</em></p>''<p><strong>Hello</strong> <em>Vue!</em></p>'
Key Moments - 3 Insights
Why does the HTML inside v-html render as formatted text instead of plain text?
Because v-html sets the element's innerHTML property, the browser parses the string as HTML tags, not plain text. See execution_table step 3.
Can we use v-html to insert user input safely?
No, inserting raw HTML can cause security risks like XSS. Always sanitize user input before using v-html. This is important beyond the shown steps.
What happens if rawHtml changes after initial render?
Vue updates the innerHTML of the element with the new string, re-parsing and re-rendering the HTML. This dynamic update is similar to step 2 repeated.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the DOM change at step 2?
ABrowser creates new Vue component
Bdiv innerHTML is set to the rawHtml string
Cdiv is removed from DOM
DrawHtml variable is cleared
💡 Hint
Check the 'DOM Change' column in step 2 of the execution_table.
At which step does the browser parse the inserted HTML string into elements?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step describing browser parsing in the execution_table.
If rawHtml changes to '<h1>Title</h1>', what will happen in the execution flow?
AVue updates innerHTML and browser parses new HTML
BNothing changes because v-html only runs once
CVue throws an error
DThe div is removed from DOM
💡 Hint
Refer to key_moments about dynamic updates and execution_table step 2.
Concept Snapshot
v-html inserts raw HTML string into an element's innerHTML.
Vue replaces element content with parsed HTML.
Use carefully to avoid security risks.
Updates to bound variable re-render HTML.
Useful for trusted HTML content display.
Full Transcript
This visual trace shows how Vue's v-html directive works. First, Vue reads the template and finds the v-html directive with a raw HTML string. Then Vue sets the element's innerHTML to that string. The browser parses this string as HTML tags, creating elements inside the div. Finally, the formatted HTML is rendered on screen. The rawHtml variable holds the HTML string and can update dynamically, causing Vue to re-insert and re-parse the HTML. Beginners often wonder why the HTML renders formatted and not as plain text; this is because innerHTML parses the string as HTML. Also, using v-html with user input can be unsafe without sanitization. The execution table and variable tracker help visualize these steps clearly.