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

javascript - Vue is automatically escaping html characters - Stack Overflow

programmeradmin0浏览0评论

here's one of my puted methods:

            filtered() {
                return this.groups.map(group => {
                    return group.replace(this.search, '<span class="has-background-primary">' + this.search + '</span>');
                })
            }

This is supposed to highlight text in a searchbox, but the < is escaped to &lt;. What should i do to suppress escaping or how can I do it better?

here's one of my puted methods:

            filtered() {
                return this.groups.map(group => {
                    return group.replace(this.search, '<span class="has-background-primary">' + this.search + '</span>');
                })
            }

This is supposed to highlight text in a searchbox, but the < is escaped to &lt;. What should i do to suppress escaping or how can I do it better?

Share Improve this question asked Oct 3, 2018 at 16:07 Zbyszek KisłyZbyszek Kisły 2,2586 gold badges30 silver badges53 bronze badges 3
  • 1 wha r u trying to do because as it looks from here you can do this with v-if – Reşit Körsu Commented Oct 3, 2018 at 16:09
  • I'm trying to highlight searched text. if I have 'mozambik' in the list and 'zam' in searchbox i want to have moZAMbik, where caps letter are highlighted so it should look like: Mo<span class="has-background-primary">zam</span>bik – Zbyszek Kisły Commented Oct 3, 2018 at 16:11
  • 1 Might be similar to this. stackoverflow./questions/30877491/vue-display-unescaped-html – Stephen Gilboy Commented Oct 3, 2018 at 16:27
Add a ment  | 

1 Answer 1

Reset to default 6

You're on the right track. The only thing missing is a v-html at the place where you render your result/list.

<div v-for="item in items" v-html="item">
  <!-- if the item now contains raw html it will not be escaped -->
</div>

I've created a small fiddle for demonstration: http://jsfiddle/6bto2nkv/

发布评论

评论列表(0)

  1. 暂无评论