Vue 3 v-html Directive
v-html The directive is used to update the element's innerHTML, rendering content as raw HTML.
Basic Explanation
v-html The directive inserts the value of its bound expression into the element as HTML. This is useful when you need to render rich text content.
- Expected type: string
- Purpose: updates the element's innerHTML, rendering raw HTML.
Use Cases
1. Render Rich Text Content
When fetching HTML content from the server and needing to render it:
Example
Article content
Security Warning
Security Issue: Dynamically rendering arbitrary HTML may lead to XSS (Cross-Site Scripting) attacks. Always ensure v-html content is trusted; never use v-html on user-submitted content.
Difference from v-text
| Instruction | Purpose | Example |
|---|---|---|
| v-text | renders content as plain text | v-text="'testdisplays test |
| v-html | parses content as HTML | v-html="'testdisplays test (bold) |
Vue 3 Built-in Directives