2

Just wondering if there is something I'm missing here:

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

doens't appear to work.

I would like to have this span interpreting HTML only if shouldParseHTMLBool is set to true.

Is it something possible, or should I use a v-if? In this trivial example it's not a big deal, but with my components I'll have to duplicate ~30 lines for each condition.

1
  • v-html is going to replace whatever is in the element with it's value. So it would make more sense for the contentWithoutParsedHTML to be in the place of HTMLContent, or as the else of the ternary Commented Feb 11, 2019 at 19:42

3 Answers 3

2

It's better to make if condition away from template as much as possible. You should create a computed object instead.

[template]

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

[script]

... computed: { computedContent: function () { return this.shouldParseHTMLBool ? this.HTMLContent : '' } }, ... 
Sign up to request clarification or add additional context in comments.

Comments

0

The v-html directive replaces the innerHTML of the element. In your case, the {{ contentWithoutParsedHTML }} will be replaced with the value of (shouldParseHTMLBool ? HTMLContent : '')

You can do something like

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

Comments

0

try this

<span v-if="shouldParseHTMLBool" v-html="HTMLContentForIfCondition" >All</span> <span v-else v-html="HTMLContentForElseCondition" ></span> 

You can use two spans, One for v-if is true and other for v-else

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.