【问题标题】:Rendering html tags in Vue.js在 Vue.js 中渲染 html 标签
【发布时间】:2018-07-19 19:25:16
【问题描述】:

我有以下代码:

HTML

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

JS

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

我想要这个渲染:

你好你好

但它会呈现标签本身而不将其转换为上标。 JSfiddle:http://jsfiddle.net/agreyfield91/eywraw8t/188244/

有没有办法通过 Vue.js 方法添加 html 标签?

【问题讨论】:

标签: javascript vue.js


【解决方案1】:

您需要绑定它,而不是渲染 html:

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

在你的情况下:

<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>

【讨论】:

  • 我需要它都在方法中。是否可以在不编辑 html 的情况下执行此操作?
猜你喜欢
  • 2016-10-01
  • 2018-12-09
  • 2011-09-17
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
相关资源
最近更新 更多