Display unescaped HTML in Vue.js

JavascriptHtmlvue.js

Javascript Problem Overview


How can I manage to get HTML interpreted inside a mustache binding? At the moment the break (<br />) is just displayed/escaped.

Small Vue app:

var logapp = new Vue({
  el: '#logapp',
  data: {
    title: 'Logs',
    logs: [
      { status: true, type: 'Import', desc: 'Learn<br />JavaScript', date: '11.11.2015', id: 1  },
      { status: true, type: 'Import', desc: 'Learn<br />JavaScript', date: '11.11.2015', id: 1  }
    ]
  }
})

And here is the template:

<div id="logapp">    
    <table>
    	<tbody>
    		<tr v-repeat="logs">
    			<td>{{fail}}</td>
    			<td>{{type}}</td>
    			<td>{{description}}</td>
    			<td>{{stamp}}</td>
    			<td>{{id}}</td>
    		</tr>
    	</tbody>
    </table>
</div>

Javascript Solutions


Solution 1 - Javascript

You can use the v-html directive to show it. like this:

<td v-html="desc"></td>

Solution 2 - Javascript

Starting with Vue2, the triple braces were deprecated, you are to use v-html.

<div v-html="task.html_content"> </div>

It is unclear from the documentation link as to what we are supposed to place inside v-html, your variables goes inside v-html.

Also, v-html works only with <div> or <span> but not with <template>.

If you want to see this live in an app, click here.

Solution 3 - Javascript

You can read that here

If you use

{{<br />}}

it'll be escaped. If you want raw html, you gotta use

{{{<br />}}}

EDIT (Feb 5 2017): As @hitautodestruct points out, in vue 2 you should use v-html instead of triple curly braces.

Solution 4 - Javascript

You have to use v-html directive for displaying html content inside a vue component

<div v-html="html content data property"></div>

Solution 5 - Javascript

Vue by default ships with the v-html directive to show it, you bind it onto the element itself rather than using the normal moustache binding for string variables.

So for your specific example you would need:

<div id="logapp">    
    <table>
        <tbody>
            <tr v-repeat="logs">
                <td v-html="fail"></td>
                <td v-html="type"></td>
                <td v-html="description"></td>
                <td v-html="stamp"></td>
                <td v-html="id"></td>
            </tr>
        </tbody>
    </table>
</div>

Solution 6 - Javascript

Before using v-html, you have to make sure that the element which you escape is sanitized in case you allow user input, otherwise you expose your app to xss vulnerabilities.

More info here: https://vuejs.org/v2/guide/security.html

I highly encourage you that instead of using v-html to use this npm package

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeView Question on Stackoverflow
Solution 1 - Javascript王开朗View Answer on Stackoverflow
Solution 2 - JavascriptthewhitetulipView Answer on Stackoverflow
Solution 3 - JavascriptzeratulmdqView Answer on Stackoverflow
Solution 4 - JavascriptShair HaiderView Answer on Stackoverflow
Solution 5 - Javascriptuser10261970View Answer on Stackoverflow
Solution 6 - JavascriptLastM4NView Answer on Stackoverflow