Vue js text highlight filter

前端 未结 4 547
甜味超标
甜味超标 2020-12-31 08:55

I need help writing a text highlight filter using vuejs. The idea is to loop through a given array of words and if there is a match, apply a span with a class to that word.

4条回答
  •  梦谈多话
    2020-12-31 09:28

    As Jeff just said, the basic mustaches interprets the data as plain text.

    You can add your span by replacing the query with the String.replace() method.

    Here's a basic example: https://jsfiddle.net/0jew7LLz/

    Vue.filter('highlight', function(words, query) {
        return words.replace(query, '' + query + '')
    });
    

提交回复
热议问题