Vue实现的github搜索小案例

效果

 前置知识

  • vue基础
  • axios
  • es6基础语法

 代码

 index.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="stylesheet" href="<%= BASE_URL %>css/bootstrap.css" />
    <!-- 配置网页标题 -->
    <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>

main.js

//引入vue
import Vue from "vue";
//引入App
import App from "./App.vue";
//关闭Vue的生产提示
Vue.config.productionTip = false;

//创建vm
new Vue({
  el: "#app",
  render: (h) => h(App),
  beforeCreate() {
    Vue.prototype.$bus = this;
  },
});

App.vue

<template>
	<div class="container">
		<Search/>
		<List/>
	</div>
</template>

<script>
	import Search from './components/Search'
	import List from './components/List'
export default {
	name:'App',
	components:{Search,List}
}
</script>

<style>

</style>

List.vue

<template>
	<div class="row">
		<!-- 展示用户列表 -->
		<div v-show="info.users.length" class="card" v-for="user in info.users" :key="user.login">
			<a :href="user.html_url" target="_blank">
				<img :src="user.avatar_url" style='width: 100px'/>
			</a>
			<p class="card-text">{{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>
</template>

<script>
	export default {
		name:'List',
		data() {
			return {
				info:{
					isFirst:true,//是否是第一次打开页面
					isLoading:false,//是否在等待
					errMsg:'',
					users:[]//查询到的用户们
				}
			}
		},
		mounted() {
			this.$bus.$on('updateListData',(dataObj)=>{
				//es6语法 将这两个对象合并成一个对象
				this.info = {...this.info,...dataObj}
			})
		},
	}
</script>

<style scoped>
	.album {
		min-height: 50rem; /* Can be removed; just added for demo purposes */
		padding-top: 3rem;
		padding-bottom: 3rem;
		background-color: #f7f7f7;
	}

	.card {
		float: left;
		width: 33.333%;
		padding: .75rem;
		margin-bottom: 2rem;
		border: 1px solid #efefef;
		text-align: center;
	}

	.card > img {
		margin-bottom: .75rem;
		border-radius: 100px;
	}

	.card-text {
		font-size: 85%;
	}
</style>

Search.vue

<template>
		<section class="jumbotron">
		<h3 class="jumbotron-heading">Search Github Users</h3>
		<div>
			<input type="text" placeholder="enter the name you search" v-model="keyWord"/>&nbsp;
			<button @click="searchUsers">Search</button>
		</div>
	</section>
</template>

<script>
	import axios from 'axios'
export default {
	name:'Search',
	data(){
		return{
			keyWord:''
		}
	},
	methods:{
		searchUsers(){
			//请求更新List的数据
			//这里利用vue全局事件总线 注册
			this.$bus.$emit('updateListData',{isLoading:true,errMsg:'',users:[],isFirst:false})
			//github给我们提供了一个接口https://api.github.com/search/users?q=???
			//这里利用es6模板字符串
			axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
				response=>{
					console.log('请求成功了');
					//请求成功后更新List的数据
					this.$bus.$emit("updateListData",{isLoading:false,errMsg:'',users:response.data.items})
				},
				error=>{
					//请求后更新List的数据
					this.$bus.$emit('updateListData',{isLoading:false,errMsg:error.message,users:[]})
				}
			)
		}
	}
}
</script>

<style>

</style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Github上有很多可以学习的Vue实战项目。其中一些比较受欢迎的项目包括: 1. Vue.js HackerNews:这是一个基于Vue.js的HackerNews客户端,可以帮助你学习Vue.js的基本用法和组件化开发。 2. Vue.js TodoMVC:这是一个使用Vue.js实现的经典Todo应用程序,可以帮助你学习Vue.js的状态管理和组件通信。 3. Vue.js Shopping Cart:这是一个使用Vue.js和Vuex实现的购物车应用程序,可以帮助你学习Vue.js的状态管理和数据流。 4. Vue.js Weather App:这是一个使用Vue.js和第三方API实现的天气应用程序,可以帮助你学习Vue.js的异步请求和数据展示。 5. Vue.js Blog:这是一个使用Vue.js和Vue Router实现的博客应用程序,可以帮助你学习Vue.js的路由和页面导航。 你可以根据自己的实际情况选择适合你的项目进行学习。如果你对Vue.js还不太熟悉,可以先学习一些基础知识,比如Vue的基本语法、组件化开发和状态管理。你可以参考我最近开源的springboot-guide项目,该项目提供了一些关于Spring Boot的学习资源,可以帮助你入门。\[2\] #### 引用[.reference_title] - *1* *3* [Vue项目实战——实现GitHub搜索案例(学以致用,两小时带你巩固和强化Vue知识点)](https://blog.csdn.net/qq_45902692/article/details/126751629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Github 上热门的 Spring Boot 项目实战推荐](https://blog.csdn.net/hollis_chuang/article/details/102597814)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值