0
0
Vueframework~20 mins

Raw HTML with v-html in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
v-html Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Vue component render?
Consider this Vue 3 component using v-html. What will be the visible output in the browser?
Vue
<template>
  <div v-html="rawHtml"></div>
</template>

<script setup>
import { ref } from 'vue'
const rawHtml = ref('<p><strong>Hello</strong> <em>World</em>!</p>')
</script>
AThe text 'Hello World!' with 'Hello' bold and 'World' italicized.
BThe raw string '<p><strong>Hello</strong> <em>World</em>!</p>' displayed as plain text.
CA syntax error preventing the component from rendering.
DAn empty div with no visible content.
Attempts:
2 left
💡 Hint
Remember that v-html renders the string as HTML, not plain text.
📝 Syntax
intermediate
1:30remaining
Which option correctly binds raw HTML using v-html?
You want to display raw HTML stored in a Vue ref called content. Which of these is the correct syntax?
A<div v-html='{{ content }}'></div>
B<div v-html="content"></div>
C<div :v-html="content"></div>
D<div v-html="{ content }"></div>
Attempts:
2 left
💡 Hint
v-html is a directive and expects a JavaScript expression without mustache syntax.
🔧 Debug
advanced
2:30remaining
Why does this v-html binding not update when the data changes?
Given this Vue 3 component, why does the displayed HTML not update when rawHtml changes?
Vue
<template>
  <div v-html="rawHtml"></div>
  <button @click="changeHtml">Change HTML</button>
</template>

<script setup>
import { ref } from 'vue'
const rawHtml = ref('<p>Original</p>')

function changeHtml() {
  rawHtml = '<p>Updated</p>'
}
</script>
ABecause the function assigns a new string to the ref variable instead of updating its value property.
BBecause v-html does not react to changes in ref variables.
CBecause the button click event is not properly bound.
DBecause rawHtml is not declared as a reactive variable.
Attempts:
2 left
💡 Hint
Remember how to update the value inside a ref in Vue 3.
🧠 Conceptual
advanced
1:30remaining
What is a major security risk when using v-html with user input?
Why should you be cautious about using v-html to render HTML content that comes from users?
AIt will cause Vue to crash if the HTML is too long.
BIt disables all CSS styles on the page.
CIt automatically sanitizes the HTML, so there is no risk.
DIt can lead to Cross-Site Scripting (XSS) attacks if malicious scripts are injected.
Attempts:
2 left
💡 Hint
Think about what happens if someone inserts a