最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Render an element within double curly brackets {{ }} in Vue.js - Stack Overflow

programmeradmin7浏览0评论

I am trying to do the following:

{{ limit_by===4 ? '<i class="fas fa-plus"></i>Visa fler...' : 'Visa färre' }}

However Vue does not accept that i add another element <i> inside the {{ }}. How do I get around this? \<i does not work...

I am trying to do the following:

{{ limit_by===4 ? '<i class="fas fa-plus"></i>Visa fler...' : 'Visa färre' }}

However Vue does not accept that i add another element <i> inside the {{ }}. How do I get around this? \<i does not work...

Share Improve this question edited Aug 5, 2020 at 21:52 Boussadjra Brahim 1 asked Aug 5, 2020 at 21:38 Line in LinusLine in Linus 4201 gold badge12 silver badges27 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You can use v-if and v-else as follows:

new Vue({
     el: '#app',
     data(){
          return{
               limit_by:4
          }
     }
});
<script src="https://unpkg./vue/dist/vue.min.js"></script>
<script src="https://kit.fontawesome./a076d05399.js"></script>
<div id="app">
     <p v-if="limit_by===4">
          <i class="fas fa-plus"></i>Visa fler...
     </p>
     <p v-else>
          Visa färre
     </p>
</div>

You can check the docs for more details.

Try to use conditional rendering with v-if and v-else directives :

<template v-if="limit_by===4">
   <i class="fas fa-plus"></i>Visa fler...
</template>
<template v-else>
   Visa färre
</template>

It is important to note that the use of double mustaches interprets the data as plain text, not html. I'm paraphrasing here but check out the v-html directive that is packaged with Vue on this page.

Using the v-html directive means you could still maintain the use of your ternary statement as follows:

<div v-html="limit_by===4 ? '<i class="fas fa-plus"></i>Visa fler...' : 'Visa färre'"></div>
发布评论

评论列表(0)

  1. 暂无评论