Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering html tags in Vue.js

I have the following code:

HTML

<div id="app">
  <h1>
  Hello {{superscript('hello')}}
  </h1>
</div>

JS

new Vue({
  el: "#app",
  data: {
  },
  methods: {
    superscript(input) {
        return '<sup>' + input + '</sup>'
    }
  }
})

I want this to render:

Hello hello

But instead it renders the tags themselves without turning it into a superscript. JSfiddle: http://jsfiddle.net/agreyfield91/eywraw8t/188244/

Is there a way to add html tags through a Vue.js method?

like image 822
user7548189 Avatar asked Jul 19 '18 19:07

user7548189


1 Answers

Instead of rendering the html, you need to bind it:

{{ result }}  => <span v-html="result"></span>

In your case:

<div id="app">
  <h1>
  Hello <span v-html="superscript('hello')"></span>
  </h1>
  <h1>
  What I want it to look like:   Hello <sup>hello</sup>
  </h1>
</div>
like image 172
tika Avatar answered Sep 19 '22 11:09

tika