Skip to Content
Welcome to yujie-code-blogs build by Nextra 4.0 🎉
前端八股文合集VueVue渲染模板时如何保留模板中的html注释

2. Vue渲染模板时如何保留模板中的html注释

VUE渲染模板过程中,默认情况下,html注释是不保留的

<template> <div class="Vhtml" v-html="htmlContent"></div> </template> <script setup lang="ts" name="Vhtml"> import { ref } from 'vue'; const htmlContent = ref('<!-- This is a HTML comment -->'); </script> <style scoped lang="scss"></style>
  • 注意事项

需要注意安全问题,v-html是直接插入html片段,会导致xss攻击,我们需要确保我们插入的html是安全的

  • vue在模板编译的过程会去掉我们不必要的内容
  • 需求不多,所以更多时候用不到