Vue template - convert HTML special characters (numberic) to symbols?

ぐ巨炮叔叔 提交于 2020-01-14 09:56:29

问题


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?


回答1:


The best option is using v-html actually:

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

No need of any other libs.




回答2:


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/



来源:https://stackoverflow.com/questions/46874504/vue-template-convert-html-special-characters-numberic-to-symbols

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