简体   繁体   English

v-html 内容未正确呈现

[英]v-html content not being rendered correctly

v-html content not being rendered correctly, altohught when I remove v-html in first li it works fine. v-html 内容未正确呈现,altohught 当我在第一个 li 中删除 v-html 它工作正常。

Here's my code.这是我的代码。

<div>

  <br>
  <li v-html="`a`">
    <ul>
      <li v-html="`c`"> b </li>
    </ul>
  </li>
  <br>

</div>

why 'c' nor 'b' are not being rendered, what am I doing wrong?为什么不呈现“c”和“b”,我做错了什么?

v-html renders some given HTML as the content of the tag the v-html directive is used on. v-html 将一些给定的 HTML 呈现为使用 v-html 指令的标签的内容 You can't use both v-html and child elements and have them work together, the v-html in this instance overrides the child elements.您不能同时使用 v-html 和子元素并让它们一起工作,在这种情况下 v-html 会覆盖子元素。

v-html basically said - keep this element's inner HTML up-to-date with the rawHtml property on the current active instance. v-html基本上是说 -使用当前活动实例上的 rawHtml 属性使该元素的内部 HTML 保持最新。

Hence, The contents of the li will be replaced with the value of the rawHtml property passed in the v-html .因此, li的内容将替换为v-html中传递的 rawHtml 属性的值。 That's the reason you are not able to see the inner HTML content of li .这就是您看不到li的内部 HTML 内容的原因。

Also, No need to use backticks as v-html is a predefined directive by Vue and it will accept the property as a rawHtml.此外,无需使用反引号,因为v-html是 Vue 的预定义指令,它将接受该属性作为 rawHtml。

Live Demo :现场演示

 new Vue({ el: '#app', data: { a: '<ul>Render outer li Html</ul>', c: '<ul>Render inner li Html</ul>' } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <li v-html="a"> <ul> <li v-html="c">b</li> </ul> </li> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM