代码
<template>
<div>
<div v-if = '!repoUrl'>loading</div>
<div v-else>most star repo is <a :href= 'repoUrl'>{{repoName}}</a></div>
</div>
</template>
<script>
import axios from 'axios'
export default{
data(){
return{
repoUrl:'',
repoName:''
}
},
created(){
//发起ajax请求并获取数据
const url = 'https://api.girhub.com'
//使用axios发送ajax请求
axios.get(url).then(
response => {
//进行到这一步表示成功了
const result = response.data
//得到最受欢迎 的repo
const mostRepo = result.items[0]
this.repoUrl = mostRepo.html_url
this.repoName = mostRepo.name
}
).catch(
error => {
alert('请求失败' + error)//声明的变量必须使用
}
)
}
}
</script>
<style>
</style>