Vue-51、Vue技术github案例(发送ajax)

1、在index引入bootstrap.csss (注意第三方css库最好在indxe里面引入)

2、List.vue源码

javascript 复制代码
<template>
        <div class="row">
            <div v-show="users.length" class="card" v-for="p in users" :key="p.login">
                <a :href="p.html_url" target="_blank">
                    <img :src="p.avatar_url" style='width: 100px'/>
                </a>
                <p class="card-text">p.login</p>
            </div>

<!--            展示欢迎词-->
            <h1 v-show="isFirst">欢迎使用</h1>

            <h1 v-show="isLoading">加载中</h1>

            <h1 v-show="errMsg">{{errMsg}}</h1>
        </div>
</template>
<script>
    export default {
        name: "List",
        data(){
            return{
                users:[],
                isFirst:true,
                isLoading:false,
                errMsg:''
            }
        },
        methods:{
            getUserList(data,isFirst,isLoading,errMsg){
                this.users = data;
                this.isFirst= isFirst;
                this.isLoading = isLoading;
                this.errMsg = errMsg;
            }
        },
        mounted() {
            this.$bus.$on('getList',this.getUserList);
        },
        beforeDestroy() {
            this.$bus.$off('getList');
        }
    }
</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>

3、Search.vue源码

javascript 复制代码
<template>
    <div>
        <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="search">Search</button>
            </div>
        </section>
    </div>
</template>
<script>
    //接口地址  https://api.github.com/search/users?q=xxx
    import axios from 'axios';
    export default {
        name: "Search",
        data(){
          return{
              keyWord:'',
          }
        },
        methods:{
            search(){
                //请求前更新数据List数据
                this.$bus.$emit('getList',[],false,true,'');
                axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
                    response=>{
                        console.log(response.data.items);
                        this.$bus.$emit('getList',response.data.items,false,false,'');
                        console.log(response.data.items);
                    },error=>{
                        console.log("请求失败");
                        this.$bus.$emit('getList',[],false,false,error.message);
                    }
                )
            }
        }
    }
</script>

<style scoped>

</style>
相关推荐
萌萌哒草头将军8 小时前
🚀🚀🚀 不要只知道 Vite 了,可以看看 Farm ,Rust 编写的快速且一致的打包工具
前端·vue.js·react.js
前端南玖10 小时前
深入Vue3响应式:手写实现reactive与ref
前端·javascript·vue.js
不一样的少年_12 小时前
头像组件崩溃、乱序、加载失败?一套队列机制+多级兜底全搞定
前端·vue.js
狙击13 小时前
基于Vue3的H5自定义拍身份证人相框和国徽框
vue.js
rookiefishs14 小时前
一个在electron中强制启用触摸模式🤚🏻的方法
前端·javascript·vue.js
深呼吸99315 小时前
如何用div手写一个富文本编辑器(contenteditable="true")
前端·vue.js
qianmoQ15 小时前
GitHub 趋势日报 (2025年06月14日)
github
BillKu15 小时前
Vue3 axios 请求设置 signal 信号属性,以便 abort 取消请求
前端·javascript·vue.js
梓羽玩Python17 小时前
12K+ Star的离线语音神器!50MB模型秒杀云端API,隐私零成本,20+语种支持!
人工智能·python·github
掘金安东尼17 小时前
9 个【宝藏工具】精选,大幅提升效率与灵感!
前端·面试·github