美文网首页
Github 搜索案例

Github 搜索案例

作者: 洪锦一 | 来源:发表于2022-03-22 08:29 被阅读0次

使用 axios

npm i axios --save

使用时事件总线

main.js
Vue.prototype.$bus = new Vue()

List组件

<template>
  <div>
    <div class="list_wrap">
      <!-- 展示用户列表 -->
      <div
        v-show="info.users.length"
        class="item_wrap"
        v-for="user in info.users"
        :key="user.id">
        <a :href="user.html_url" target="_blank">
          <img :src="user.avatar_url" alt="" />
        </a>
        <p>{{ user.login }}</p>
      </div>

      <!-- 展示欢迎词 -->
      <h1 v-show="info.isFirst">欢迎使用!</h1>
      <!-- 展示加载中 -->
      <h1 v-show="info.isLoading">加载中...</h1>
      <!-- 展示错误信息 -->
      <h1 v-show="info.errMsg">请求错误,{{ info.errMsg }}</h1>
    </div>
  </div>
</template>

<script>
export default {
  name: "List",
  data() {
    return {
      info: {
        isFirst: true, //是否第一次加载
        isLoading: false, // 是否加载中
        errMsg: "", // 错误信息
        users: [], //用户列表
      },
    };
  },
  mounted() {
    this.$bus.$on("updateListData", (dataObj) => {
      console.log(dataObj);
      this.info = { ...this.info, ...dataObj };
    });
  },
};
</script>

<style lang="less" scoped>
.list_wrap {
  width: 80%;
  margin: auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  &::after {
    content: "";
  }
  .item_wrap {
    text-align: center;
    margin-top: 20px;
    padding: 0 10px;

    img {
      width: 80px;
      height: 80px;
    }
    p {
      width: 80px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
}
</style>

Search组件

<template>
  <div>
    <div class="search_wrap">
      <input type="text" v-model="keyWord" />
      <button @click="handleSerch">搜索</button>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "Search",
  data() {
    return {
      keyWord: 0,
    };
  },
  methods: {
    handleSerch() {
      // 请求前更新list数据
      this.$bus.$emit("updateListData", {
        isFirst: false,
        isLoading: true,
        errMsg: "",
        users: [],
      });

      axios.get(`https://api.github.com/1search/users?q=${this.keyWord}`).then(
        (response) => {
          this.$bus.$emit("updateListData", {
            isLoading: false,
            errMsg: "",
            users: response.data.items,
          });
        },
        (error) => {
          this.$bus.$emit("updateListData", {
            isLoading: false,
            errMsg: error.message,
            users: [],
          });
        }
      );
    },
  },
};
</script>

<style>
.search_wrap {
  width: 80%;
  background-color: rgb(221, 221, 221);
  margin: auto;
  text-align: center;
  padding: 30px 0;
}
</style>

App组件

<template>
  <div>
    <Search />
    <List />
  </div>
</template>

<script>
import Search from "./components/Search.vue";
import List from "./components/List.vue";

export default {
  name: "App",
  components: {
    Search,
    List,
  },
};
</script>

<style>
* {
  padding: 0;
  margin: 0;
}
</style>

相关文章

  • Github 搜索案例

    使用 axios npm i axios --save 使用时事件总线 main.jsVue.prototype....

  • GitHub 搜索

    GitHub 的 搜索 做的不错,这里列一下方便使用; 搜索什么? |范围|备注||-|-|-||Reposito...

  • github搜索

    #按照项目名/仓库名搜索(大小写不敏感) in : name xxx #按照 README 搜索(大小写不敏感) ...

  • Vue实现简单的搜索github用户案例

    目录 前言 这个案例主要是对与Vue学习的一个小整合,里面使用了Bootstrap5调整样式,使用了axios进行...

  • json-server

    github 搜索

  • Android 开源组件和第三方库汇总

    1、 github排名 https://github.com/trending,github搜索:https://...

  • Github 搜索技巧

    Github 的资源对于广大开发者来说真是个宝藏,那么除了在搜索框里输入关键字再回车之外,我们还可以怎么用呢?一般...

  • Github 搜索技巧

    搜索一些好东西: Awesome + 你的关键字 github中国区排名: location:china 点...

  • github搜索简介

    -----程序员遇到问题,或者想学习什么,第一个念头可能都是百度/谷歌。然而作为全球最大的代码托管平台,githu...

  • Github搜索技巧

    # Github搜索技巧在search栏搜索开源项目##名字搜索`In:name spring boot` **搜...

网友评论

      本文标题:Github 搜索案例

      本文链接:https://www.haomeiwen.com/subject/lwfndrtx.html