写一个githubDemo

1.List组件

html 复制代码
<template>
  <div class="container">
    <!-- 展示用户列表 -->
    <div class="row">
      <div
        v-show="info.users.length"
        v-for="(item, index) in info.users"
        :key="item.id"
      >
        <div class="col-sm-4 item">
          <img
            class="hover-image"
            :src="item.avatar_url"
            @click="targetPage(item.html_url)"
          />
          <h5>{{ item.login }}</h5>
        </div>
        <br v-if="isHuanhang(index)" />
      </div>
    </div>

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

<script>
export default {
  name: "MyList",
  data() {
    return {
      info: {
        users: [],
        isFirst: true,
        isLoading: false,
        errMsg: "",
      },
    };
  },

  methods: {
    getList(listObj) {
      //1.第一种方式
      // this.info = {...this.info,...listObj};
      //2.第二种方式
      Object.assign(this.info, listObj);
    },
    isHuanhang(index) {
      if (index % 3 === 0) {
        return true;
      } else {
        return false;
      }
    },
    targetPage(url) {
      window.open(url, "_blank");
    },
  },
  mounted() {
    this.$bus.$on("sendList", this.getList);
  },
  beforeDestroy() {
    this.$bus.$off("sendList");
  },
};
</script>

<style scoped>
.item {
  width: 300px;
  height: 150px;
  margin-top: 30px;
  margin-right: 76px;
  padding: 150px;
  display: flex;
  flex-direction: column; /* 纵向排列子项 */
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  border: 1px solid #ccc; /* 可选:给容器加个边框 */
  box-sizing: border-box; /* 包括边框和内边距在内的总宽高 */
}

img {
  width: 150x;
  height: 120px;
}

.hover-image {
  cursor: pointer; /* 设置鼠标悬浮时光标变成手指 */
}
</style>

2.search 组件

html 复制代码
<template>
  <div class="container-xl">
    <div class="row bc">
      <div
        class="col-xl search-box d-flex flex-column justify-content-end"
      >
        <h1 class="text-bottom margin-left-dom">Search Github Users</h1>
      </div>
    </div>
    <div class="row bc">
      <div class="col-xl search-box" >
        <div class="input-group mb-3 margin-left-dom inputWidth">
          <input
            type="text"
            class="form-control"
            placeholder="enter the name you search"
            v-model="keyWord"
          />
            <button class="btn btn-outline-dark dom-margin" type="button" @click="searchUsers" >Search</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';


export default {
  name: "MySearch",
  data() {
    return {
      keyWord:''
    }
  },
  methods:{
    searchUsers(){
         this.$bus.$emit('sendList',{ 
        isFirst: false,
        isLoading: true,
        });


      axios.get('https://api.github.com/search/users',{
        params:{
          q:this.keyWord
        }
      }).then(res=>{
         this.$bus.$emit('sendList',{ 
          users: res.data.items,
        isLoading: false,
        });
      },err=>{
        console.log(err);
        this.$bus.$emit('sendList',{ 
            users: [],
            isLoading: false,
            errMsg: '请求失败,请稍后再试!'
        });
      })
    }

  }
};
</script>

<style scoped>

.bc{
background-color: rgba(13, 14, 14, 0.313);

}

.search-box {
  height: 120px;
}

.margin-left-dom {
  margin-left: 30px;
}

.inputWidth {
  width: 450px;
}
.dom-margin{
  margin-left: 12px;
}
</style>

3.App组件

html 复制代码
<template>
  <div id="appContainer">

    <MySearch/>
    <MyList/>
    
  </div>
</template>

<script>

 import MySearch from './components/MySearch.vue'
 import MyList from './components/MyList.vue'
export default {
  name: "App",
  components: {
    MySearch,MyList
  },
  methods:{
 
  }
};
</script>

<style>

</style>

4.引入bootStarp

html 复制代码
<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <!-- 针对IE浏览器 得一个特殊配置,含义是让IE浏览器以最高得渲染级别渲染页面 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 开启移动端得理想视口 -->
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <!-- 配置页签图标 -->
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <!-- 配置网页的标题 package.json name:'vue_test'当作网页的标题 -->
     <link rel="stylesheet" href="<%= BASE_URL %>css/bootstrap.css">
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <!-- 当浏览器不支持JS时,noscript中的 元素就会被渲染 -->
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <!-- 容器 -->
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

5.展示

相关推荐
哆木8 分钟前
部署在线GBA游戏,并通过docker安装启动
游戏·html·gba
丶重明2 小时前
【2024】前端学习笔记3-外部链接-内部链接-锚点链接
html
小ᶻᶻᶻ4 小时前
HTML 和 CSS
html
软件技术NINI4 小时前
html知识点框架
前端·html
autumn8685 小时前
什么是css?
css
Estrella166 小时前
经典 web 页面排版:三栏布局
前端·css·面试
世俗ˊ7 小时前
CSS入门笔记
前端·css·笔记
6230_7 小时前
git使用“保姆级”教程1——简介及配置项设置
前端·git·学习·html·web3·学习方法·改行学it
加勒比海涛8 小时前
HTML 揭秘:HTML 编码快速入门
前端·html
茶茶只知道学习8 小时前
通过鼠标移动来调整两个盒子的宽度(响应式)
前端·javascript·css