14

I use {{}} in Vue.js to render my data to HTML. But now I got a string in my data, and I want the
tags in that string can be parsed into HTML tags when rendering data.

data(){
  return {
    bodyText: 'aaaaaa<br>aaaaaa'
}
}
<p>{{bodyText}}</p>

I want the content in span tag is like :

aaaaaa
aaaaaa

But the result is : aaaaaa<br>aaaaaa

4 Answers 4

25

I think it should work using this:

<p>Using v-html directive: <span v-html="rawHtml"></span></p>
Sign up to request clarification or add additional context in comments.

3 Comments

If you are using eslint in your project you may lead to a warning while using v-html. 'v-html' directive can lead to XSS attack vue/no-v-html to avoid this you can use it as follows- <!-- eslint-disable vue/no-v-html --> <div v-html="bodyText"></div> <!--eslint-enable-->
I wonder about the security implications of this, seems like a valid warning to me.
of course, in this case you have to make sure that no malicious code can be inserted. But there are definitely use-cases where rawHTML is the right way.
18

Use v-html directive:

<p><span v-html="bodyText"></span></p>

Comments

1

You can make it still simple by using inside the p tag itself

<p v-html="bodyText"></p>

Comments

0

My example html

<td>
    <span v-html="getInfo(item)"></span>
</td>

script

getInfo: function (item) {
   return "One line<br />Next line"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.