vue实现搜索小功能

 更新时间:2021年11月25日 16:15  点击:676 作者:温L

本文实例为大家分享了vue实现搜索小功能的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
 </head>
 <body>
  <div id="app">
   <input type="text" v-model="keyword" placeholder="输入关键字" />
   <div class="list">
    <div class="item" v-for="item in fFruit" :key="item">
     {{item}}
    </div>
   </div>
  </div>
 </body>
 <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript">
  new Vue({
   el:"#app",
   data(){
    return{
     keyword:"",
     fruit:[
      "苹果","沙果","海棠","野樱莓","枇杷","欧楂","山楂","香梨",
      "雪梨 ","温柏","蔷薇果","花楸","杏","樱桃","桃","水蜜桃",
      "油桃","蟠桃","李子","梅子","西梅","白玉樱桃 ","黑莓",
      "覆盆子","云莓","罗甘莓","白里叶莓","草莓","菠萝莓","橘子",
      "砂糖桔","橙子","柠檬","青柠","柚子","金桔","葡萄柚","香橼",
      "佛手","指橙","黄皮果","哈密瓜","香瓜","白兰瓜","刺角瓜"
     ]
    }
   },
   computed:{
    "fFruit"(){
     // 如果关键字为空,返回所有的水果
     if(this.keyword==""){
     return this.fruit;
    }else{
     // 当Frui里面某一项文字包含keyword文字那么就把当前数据保留
     // filter过滤 返回为真保留,为false就过滤掉
     
     return this.fruit.filter(item=>{
      return item.includes(this.keyword)
     })
    }
    }
    
   }
  })
 </script>
</html>

结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/lwf0829/article/details/121516743

[!--infotagslink--]

相关文章