Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 条件V-HTML呈现_Javascript_Vue.js_Vuejs2_Vue Component - Fatal编程技术网

Javascript 条件V-HTML呈现

Javascript 条件V-HTML呈现,javascript,vue.js,vuejs2,vue-component,Javascript,Vue.js,Vuejs2,Vue Component,我只是想知道我是否遗漏了什么: <span v-html="(shouldParseHTMLBool ? HTMLContent : '')"> {{ contentWithoutParsedHTML }} </span> {{contentWithoutParsedHTML}} 似乎不起作用 仅当shouldParseHTMLBool设置为true时,我才希望此span解释HTML 这是可能的,还是应该使用v-if?在这个简单的例子中,这不是什么大问题,但是对

我只是想知道我是否遗漏了什么:

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

{{contentWithoutParsedHTML}}
似乎不起作用

仅当
shouldParseHTMLBool
设置为
true
时,我才希望此
span
解释HTML


这是可能的,还是应该使用
v-if
?在这个简单的例子中,这不是什么大问题,但是对于我的组件,我必须为每个条件复制约30行。

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

你可以这样做

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

方法:{
conditionalParse:函数(){
返回this.shouldParseHTMLBool?this.HTMLContent:'
}

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

你可以这样做

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

方法:{
conditionalParse:函数(){
返回this.shouldParseHTMLBool?this.HTMLContent:'
}

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

[模板]

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

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

[模板]

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

v-html
将用它的值替换元素中的任何内容。因此,
contentWithoutParsedHTML
将取代
HTMLContent
,或者三元
v-html
的else将用它的值替换元素中的任何内容,这将更有意义用于将
contentWithoutParsedHTML
替换为
HTMLContent
,或作为