axios做ajax请求

2020-02-06  本文已影响0人  栀心_d553

代码

<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>
上一篇 下一篇

猜你喜欢

热点阅读