DEV Community

Md Jannatul Nayem
Md Jannatul Nayem

Posted on

2

How to bind html in Vue.js

Binding html means, you can bind dynamic html inside of your template. But if you try to render content of dynamic html using mustache syntax or v-text (text binding) you will render the complete html and inner text as a complete string.
Here is the exmaple:

<script>
  export default {
    data(){
      return{
        text: `Check the difference?`,
        html: `<p>I am p tag with <b>bold</b> </p>`
      }
    }
  }
</script>

<template>
  <h1>How to Bind html in Vue.js?</h1>
  <div>{{ html }}</div>
</template>

<style>
</style>
Enter fullscreen mode Exit fullscreen mode

Then what is the solution? You can use v-html binding in vue.js. Here prefix v is reserve for vue.js. You have to use v-html attribute with a empty html tag. The value of the v-html property will be the data property that you have declared in data method.

Here is the example:

<script>
  export default {
    data(){
      return{
        text: `Check the difference?`,
        html: `<p>I am p tag with <b>bold</b> </p>`
      }
    }
  }
</script>

<template>
  <h1>How to Bind html in Vue.js?</h1>
  <div>
    <p v-html="html"></p>
  </div>
</template>

<style>
</style>
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay