pwindy  
在完成任务的同时,还需要不断“复盘”,不论你多么的忙,都需要留下时间思考,可以思考哪些地方做的好,哪些地方我们可以改进,应该如何改进,注重总结才是王道

1.背景

给搜索出来的相应的字符串,加上高亮显示。

2.思路分析

2.1.建立函数wrapperKeyword,传入两个参数

  listQuery为搜索的数据(数据结构为对象{}),其中包括title,author等字段属性
复制代码
    methods:{
        wrapperKeyword(k,v){
            function highlight(value){
                return `<span style="color: #1890ff;">${value}</span>`
            }
            if(!this.listQuery[k]){
                return v
            }else{
                return v.replace( new RegExp(this.listQuery[k],'ig'), v=>highlight(v) )
            }
        }
    }
复制代码

wrapperKeyword函数返回的是带有html格式的字符串,所以在html中,使用v-html

2.2.引用wrapperKeyword函数

    mounted(){
        this.list.forEach( book => {
            book.titleWrapper = this.wrapperKeyword("title", book.title)
            book.authorWrapper = this.wrapperKeyword("author", book.author)
        })
    },

2.3.data数据

复制代码
    data () {
        return {
            list:[
                {tite:'this is title1',author:'this is author1',content:'this is content1'},
                {tite:'this is title2',author:'this is author2',content:'this is content2'},
                {tite:'this is title3',author:'this is author3',content:'this is content3'},
                {tite:'this is title4',author:'this is author4',content:'this is content4'},
            ],
            listQuery: {title: "is",author: "is"} 
};
},
复制代码

2.4.html部分

    <div v-for="(item, index) in list" :key="index">
        <span v-html="item.titleWrapper">这里是题目</span>
        <span v-html="item.authorWrapper">这里是题目</span>
    </div>
posted on   pwindy  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
 
点击右上角即可分享
微信分享提示