Rendering html tags in Vue.js

时间秒杀一切 提交于 2021-02-07 14:19:37

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/51430422/rendering-html-tags-in-vue-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!