vue实现搜索小功能

2022-04-15 0 355

本文实例为大家分享了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>

结果:

vue实现搜索小功能

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

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript vue实现搜索小功能 https://www.niceym.com/20476.html