📖 Scenario: You are building a simple Vue app that shows a message containing HTML tags. You want the HTML tags to be rendered as actual HTML, not as plain text.
🎯 Goal: Build a Vue component that uses v-html to display raw HTML content stored in a data property.
📋 What You'll Learn
Create a Vue component with a data property called rawMessage containing HTML tags.
Add a configuration variable called isVisible to control if the message is shown.
Use v-html directive to render the raw HTML inside a <div>.
Add a button to toggle the visibility of the raw HTML message.
💡 Why This Matters
🌍 Real World
Displaying user-generated content or formatted messages safely in web apps, such as blog posts or comments with HTML formatting.
💼 Career
Understanding how to render raw HTML in Vue is important for building interactive and dynamic user interfaces that handle rich text content.
Progress0 / 4 steps
1
Set up the raw HTML data property
Create a Vue component with a data function that returns an object containing a property called rawMessage with the exact value: '<strong>Hello</strong>, <em>Vue Learners!</em>'.
Vue
Hint
Use ref from Vue to create a reactive variable called rawMessage with the given HTML string.
2
Add a visibility control variable
Add a new reactive variable called isVisible using ref and set it to true inside the <script setup> section.
Vue
Hint
Create isVisible as a reactive boolean variable set to true.
3
Render raw HTML with v-html
Inside the <template>, add a <div> that uses v-if="isVisible" and the v-html directive to display the content of rawMessage as raw HTML.
Vue
Hint
Use v-if="isVisible" to show the div only when visible, and v-html="rawMessage" to render the HTML content.
4
Add a button to toggle visibility
Add a <button> inside the <template> that toggles the value of isVisible when clicked. Use the @click event with an inline arrow function to set isVisible.value = !isVisible.value. The button text should be Toggle Message.
Vue
Hint
Add a button with @click event that toggles isVisible.value between true and false.
Practice
(1/5)
1. What does the v-html directive do in Vue?
easy
A. It listens for user input events.
B. It binds a JavaScript object to the element.
C. It inserts raw HTML content inside an element.
D. It applies CSS styles dynamically.
Solution
Step 1: Understand the purpose of v-html
The v-html directive 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 to v-html.
Final Answer:
It inserts raw HTML content inside an element. -> Option C
Quick Check:
v-html = inserts raw HTML [OK]
Hint: Remember: v-html inserts raw HTML inside elements [OK]
Common Mistakes:
Confusing v-html with event binding
Thinking v-html applies styles
Assuming v-html binds JavaScript objects
2. Which of the following is the correct syntax to use v-html in a Vue template?
easy
A.
B.
C.
D.
Solution
Step 1: Identify correct directive syntax
The correct directive to insert raw HTML is v-html, used as v-html="rawHtml".
Step 2: Check other options for correctness
uses a non-existent directive :html. uses v-text which inserts plain text, not HTML. uses an invalid directive v-bind-html.
Final Answer:
<div v-html="rawHtml"></div> -> Option B
Quick Check:
Correct syntax = v-html="variable" [OK]
Hint: Use v-html="variable" to insert raw HTML [OK]
Common Mistakes:
Using v-text instead of v-html
Trying v-bind-html which is invalid
Using shorthand :html which doesn't exist
3. Given the Vue template and data below, what will be rendered inside the div?