蓝桥杯 - 简单 - 搜一搜呀

目标

能够实现在输入后直接进行搜索效果

代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="vue.min.js"></script>
    <link rel="stylesheet" href="./css/style.css" />
  </head>

  <body>
    <div id="app">
      <div class="search-wrapper">
        <input type="text" v-model="search" placeholder="请搜索" />
      </div>
      <div class="wrapper">
        <div class="card" v-for="post in filteredList">
          <a v-bind:href="post.link" target="_blank">
            <img v-bind:src="post.img" />
            {
   
   { post.title }}
          </a>
        </div>
      </div>
    </div>
    <script>
      class Post {
        constructor(title, link, img) {
          this.title = title;
          this.link = link;
          this.img = img;
        }
      }

      const app = new Vue({
        el: "#app",
        data: {
          search: "",
          postList: [
            new Post(
              "小猫咪",
              "https://unsplash.com/s/photos/cat",
              "./images/cat.png"
            ),
            new Post(
              "小狗狗",
              "https://unsplash.com/@joeyc",
              "./images/dog.png"
            ),
            new Post(
              "北极熊",
              "https://unsplash.com/@hansjurgen007",
              "./images/bar.png"
            ),
            new Post(
              "狮子",
              "https://unsplash.com/@hansjurgen007",
              "./images/lion.png"
            ),
            new Post(
              "小鸟",
              "https://unsplash.com/@eugenechystiakov",
              "./images/birds.png"
            ),
            new Post(
              "狐狸",
              "https://unsplash.com/@introspectivedsgn",
              "./images/fox.png"
            ),
          ],
        },
        computed: {
          filteredList() {
            // TODO: 请补充代码
            return this.postList.filter(post=>{
              return post.title.toLowerCase().includes(this.search.toLowerCase())
            })
          },
        },
      });
    </script>
  </body>
</html>

补充代码部分

computed: {
          filteredList() {
            // TODO: 请补充代码
            return this.postList.filter(post=>{
              return post.title.toLowerCase().includes(this.search.toLowerCase())
            })
          },
        },

解题步骤

  1. .filter()是一个JavaScript数组方法,它会对数组中的每个元素执行一个函数,并根据这个函数的返回值来决定是否保留该元素。
  2. return post.title.toLowerCase().includes(this.search.toLowerCase())      
  • post.title:假设每个post对象都有一个title属性,这里获取该属性的值。
  • .toLowerCase():这是一个JavaScript字符串方法,它将字符串转换为小写。这确保了搜索时不区分大小写。
  • .includes(this.search.toLowerCase()).includes()是另一个JavaScript字符串方法,它检查字符串是否包含指定的子字符串。这里,它检查 post.title(已转换为小写)是否包含this.search(用户输入的搜索关键字,同样转换为小写

整体流程

  • 当用户在搜索框中输入文本时,this.search会更新。
  • 由于filteredList依赖于this.searchthis.postList,Vue会重新计算filteredList的值。
  • .filter()方法会遍历this.postList数组,并使用箭头函数检查每个posttitle是否包含用户输入的搜索关键字。
  • 最终,filteredList会返回一个新的数组,其中只包含那些标题与搜索关键字匹配的post对象。

复习数据的相关使用函数!!