繁体   English   中英

有条件的V-HTML渲染

[英]Conditional V-HTML rendering

只是想知道我是否在这里缺少什么:

<span v-html="(shouldParseHTMLBool ? HTMLContent : '')">
  {{ contentWithoutParsedHTML }}
</span>

似乎没有用。

我想有这种span解析HTML,如果shouldParseHTMLBool设置为true

有可能吗,还是我应该使用v-if 在这个琐碎的示例中,这没什么大不了的,但是对于我的组件,我将不得不为每个条件重复〜30行。

v-html指令替换了元素的innerHTML。 在您的情况下, {{ contentWithoutParsedHTML }}将被替换为(shouldParseHTMLBool ? HTMLContent : '')的值。

你可以做类似的事情

<template>
<span v-html="conditionalParse"></span>
</template>
<script>
methods: {
conditionalParse: function () {
    return this.shouldParseHTMLBool ? this.HTMLContent : ''
}
</script>

最好使条件尽可能远离模板。 您应该改为创建一个计算对象。

[模板]

<span v-html="computedContent"></span>

[脚本]

...
computed: {
  computedContent: function () {
    return this.shouldParseHTMLBool ? this.HTMLContent : ''
  }
},
...

暂无
暂无

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

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