Vue template - convert HTML special characters (nu

2019-06-27 12:38发布

How can I convert special characters (numberic) to symbols in my Vue template?

For instance I have this JSON data:

[{"id":"post91","slug":null,"title":"Breakfast & Tea"}]

How can I convert Breakfast & Tea to Breakfast & Tea?

My Vue template:

<h3 class="heading">{{ item.title }}</h3>

Any ideas?

2条回答
时光不老,我们不散
2楼-- · 2019-06-27 13:03

The best option is using v-html actually:

<h3 class="heading" v-html="item.title"></h3>

No need of any other libs.

查看更多
小情绪 Triste *
3楼-- · 2019-06-27 13:05

It's easier to use a library like he for this:

new Vue({
  el: '#app',
  created(){
    this.message = this.decode('Breakfast &#038; Tea');
  },
  methods:{
    decode(str){
        return he.decode(str);
    }
  },
  data:{
    message: ''
  }
})

Here's the JSFiddle: https://jsfiddle.net/86k1ge4b/

查看更多
登录 后发表回答