Skip to content
Advertisement

Use HTML entities in my block in Vue.js

I want to use the HTML-Symbols &#9650 and &#9660 that stand for “arrow up” and “arrow down” in the <script> section of my Vue.js component. I know that something simple as the following doesn’t work. But I tried different functions that I found on the internet and nothing worked.

this.$refs["span-1"].textContent = "&#9650";

Advertisement

Answer

There are two ways to achieve this :

  1. Use v-html directive

new Vue({
  el: '#app',
  data: {
    content: "&#9650"
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p v-html="content"></p>
</div>
  1. Use innerHTML instead of textContent

new Vue({
  el: '#app',
  mounted() {
    this.$refs.myTag.innerHTML = "&#9650";
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p ref="myTag"></p>
</div>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement