Vue基础18之github案例、vue-resource

news2024/9/20 22:40:48

Vue基础18

  • github案例
    • 静态页面
      • 第三方样式引入(以bootstrap举例)
      • App.vue
      • Search.vue
      • List.vue
    • 列表展示
      • 接口地址
      • 使用全局事件总线进行兄弟间组件通信
        • Search.vue
        • List.vue
    • 完善案例
      • List.vue
      • Search.vue
      • 补充知识点:{...this.info,...this.dataObj}
      • 效果呈现
  • vue-resource
    • 安装
    • 引用
      • main.js
    • 使用
      • Search.vue

github案例

静态页面

第三方样式引入(以bootstrap举例)

  1. public中创建文件夹css,将样式放入
    在这里插入图片描述

  2. 在index.html中使用link引入

<!--   引入第三方样式   -->
      <link rel="stylesheet" href="<%= BASE_URL %>css/bootstrap.css">

在这里插入图片描述

App.vue

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

<script>
import Search from "@/components/Search";
import List from "@/components/List";
export default {
  name: "App",
  components: {Search, List},
  methods:{

  }
}
</script>

<style lang="less">

</style>

Search.vue

<template>
  <div class="jumbotron bg">
    <h1>Search Github Users</h1>
    <p><input type="text" placeholder="输入你想要搜索的用户名">
      <a class="btn btn-default btn-lg" href="#" role="button">Search</a>
    </p>
  </div>
</template>

<script>
    export default {
        name: "Search"
    }
</script>

<style scoped lang="less">
.bg{
  height: 250px;
  margin: 0 20px;
  padding-left: 50px;
  padding-top: 50px;
  .title{
    font-size: 20px;
  }
  input{
    margin-right: 10px;
  }
}
</style>

List.vue

<template>
<div class="bg">
  <div class="predict" v-show="isShow">welcome to use</div>
  <div class="pro-list">
    <div class="pro-item" v-for="item in content">
      <img src="@/assets/logo.png" alt="">
      <div class="text">122555</div>
    </div>
  </div>
</div>
</template>

<script>
    export default {
        name: "List",
      data(){
          return{
            isShow:false,
            content:["","","","","","","","","",""]
          }
      }
    }
</script>

<style scoped lang="less">
.bg{
  margin: 0 20px;
  font-size: 24px;
  .pro-list{
    display: flex;
    flex-wrap: wrap;
    .pro-item{
      margin: 50px  120px;
      .text{
        text-align: center;
      }
    }
  }

}
</style>

列表展示

接口地址

https://api.github.com/search/users?q=xxx

用到的响应值:

  1. avatar_url:头像链接
  2. html_url:用户详情页
  3. login:用户名

使用全局事件总线进行兄弟间组件通信

Search.vue

<template>
  <div class="jumbotron bg">
    <h1>Search Github Users</h1>
    <p>
      <input type="text" placeholder="输入你想要搜索的用户名" v-model="keywords">
      <a class="btn btn-default btn-lg" href="#" role="button" @click="searchUsers">Search</a>
    </p>
  </div>
</template>

<script>
import axios from "axios";
    export default {
        name: "Search",
      data(){
          return{
            keywords:"",
          }
      },
      methods:{
        searchUsers(){
          if(this.keywords.trim()=="") return alert("用户输入的内容不得为空")
          axios.get(`https://api.github.com/search/users?q=`+this.keywords).then(
              response=>{
                // console.log(response.data.items)
                console.log("请求成功了~")
                this.$bus.$emit('getUsersList',response.data.items)
              },
              error=>{
                console.log(error.msg)
              }
          )

        }
      }
    }
</script>

<style scoped lang="less">
.bg{
  height: 250px;
  margin: 0 20px;
  padding-left: 50px;
  padding-top: 50px;
  .title{
    font-size: 20px;
  }
  input{
    margin-right: 10px;
  }
}
</style>

List.vue

<template>
<div class="bg">
  <div class="predict" v-show="!users.length">welcome to use</div>
  <div class="pro-list">
    <div class="pro-item" v-for="user in users" :key="user.login">
      <a :href="user.html_url" class="aitem">
        <img :src="user.avatar_url" alt="">
      </a>
      <div class="text">{{ user.login }}</div>
    </div>
  </div>
</div>
</template>

<script>
    export default {
      name: "List",
      data(){
          return{
            isShow:false,
            users:[],
          }
      },
      mounted() {
        this.$bus.$on('getUsersList',(users)=>{
          this.users=users
        })
      }
    }
</script>

<style scoped lang="less">
.bg{
  margin: 0 20px;
  font-size: 24px;
  .pro-list{
    display: flex;
    flex-wrap: wrap;
    .pro-item{
      margin: 50px  120px;
      .aitem{
        display: inline-block;
        width: 200px;
        height: 200px;
      }
      img{
        width: 200px;
        height: 200px;
      }
      .text{
        text-align: center;
      }
    }
  }

}
</style>

请添加图片描述

完善案例

在这里插入图片描述

List.vue

<template>
<div class="bg">
  <h1 class="predict" v-show="info.isFirst">欢迎使用!</h1>
  <h1 v-show="info.isLoading">加载中...</h1>
  <h1 v-show="info.emsg">错误信息:{{info.emsg}}</h1>
  <div class="pro-list">
    <div class="pro-item" v-for="user in info.users" :key="user.login">
      <a :href="user.html_url" class="aitem">
        <img :src="user.avatar_url" alt="">
      </a>
      <div class="text">{{ user.login }}</div>
    </div>
  </div>
</div>
</template>

<script>
    export default {
      name: "List",
      data(){
          return{
            isShow:false,
            users:[],
            info:{
              isFirst:true,
              isLoading:false,
              emsg:"",
              users:[]
            }
          }
      },
      mounted() {
        this.$bus.$on('updateListData',(dataObj)=>{
          this.info={...this.info,...dataObj}
        })
      }
    }
</script>

<style scoped lang="less">
.bg{
  margin: 0 20px;
  font-size: 24px;
  .pro-list{
    display: flex;
    flex-wrap: wrap;
    .pro-item{
      margin: 50px  120px;
      .aitem{
        display: inline-block;
        width: 200px;
        height: 200px;
      }
      img{
        width: 200px;
        height: 200px;
      }
      .text{
        text-align: center;
      }
    }
  }

}
</style>

Search.vue

<template>
  <div class="jumbotron bg">
    <h1>Search Github Users</h1>
    <p>
      <input type="text" placeholder="输入你想要搜索的用户名" v-model="keywords">
      <a class="btn btn-default btn-lg" href="#" role="button" @click="searchUsers">Search</a>
    </p>
  </div>
</template>

<script>
import axios from "axios";
    export default {
        name: "Search",
      data(){
          return{
            keywords:"",
          }
      },
      methods:{
        searchUsers(){
          if(this.keywords.trim()=="") return alert("用户输入的内容不得为空")
          else{
            this.$bus.$emit("updateListData",{isFirst:false,isLoading:true,emsg:"",users:[]})
            axios.get(`https://api.github.com/search/users?q=${this.keywords}`).then(
                response=>{
                  // console.log(response.data.items)
                  console.log("请求成功了~")
                  this.$bus.$emit('updateListData',{isLoading:false,emsg:"",users:response.data.items})
                },
                error=>{
                  console.log(error.msg)
                  this.$bus.$emit("updateListData",{isLoading:false,emsg:error.message,users:[]})
                }
            )
          }


        }
      }
    }
</script>

<style scoped lang="less">
.bg{
  height: 250px;
  margin: 0 20px;
  padding-left: 50px;
  padding-top: 50px;
  .title{
    font-size: 20px;
  }
  input{
    margin-right: 10px;
  }
}
</style>

补充知识点:{…this.info,…this.dataObj}

{…this.info,…this.dataObj}:通过字面量的形式合并一下对象
以this.dataObj为主,依次替换this.info中的属性的值,但是this.dataObj中没有的数据,还是沿用this.info中的

效果呈现

请添加图片描述

vue-resource

发送Ajax的5种方式:
1.xhr
2.jQuery
3.axios
4.fetch
5.vue-resource(插件库)对xhr的封装

安装

npm i vue-resource
在这里插入图片描述

引用

main.js

import Vue from 'vue'

import App from './App'
//引入vue-resource插件
import vueresource from 'vue-resource'

Vue.config.productionTip = false
//使用vue-resource插件
Vue.use(vueresource)

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

使用

与axios一致,只需要将axios替换成this.$http就行

this.$http.get(`https://api.github.com/search/users?q=${this.keywords}`).then(
                response=>{
                  // console.log(response.data.items)
                  console.log("请求成功了~")
                  this.$bus.$emit('updateListData',{isLoading:false,emsg:"",users:response.data.items})
                },
                error=>{
                  console.log(error.msg)
                  this.$bus.$emit("updateListData",{isLoading:false,emsg:error.message,users:[]})
                }
            )

Search.vue

<template>
  <div class="jumbotron bg">
    <h1>Search Github Users</h1>
    <p>
      <input type="text" placeholder="输入你想要搜索的用户名" v-model="keywords">
      <a class="btn btn-default btn-lg" href="#" role="button" @click="searchUsers">Search</a>
    </p>
  </div>
</template>

<script>
import axios from "axios";
    export default {
        name: "Search",
      data(){
          return{
            keywords:"",
          }
      },
      methods:{
        searchUsers(){
          if(this.keywords&&this.keywords.trim()=="") return alert("用户输入的内容不得为空")
          else{
            this.$bus.$emit("updateListData",{isFirst:false,isLoading:true,emsg:"",users:[]})
            this.$http.get(`https://api.github.com/search/users?q=${this.keywords}`).then(
                response=>{
                  // console.log(response.data.items)
                  console.log("请求成功了~")
                  this.$bus.$emit('updateListData',{isLoading:false,emsg:"",users:response.data.items})
                },
                error=>{
                  console.log(error.msg)
                  this.$bus.$emit("updateListData",{isLoading:false,emsg:error.message,users:[]})
                }
            )
          }


        }
      }
    }
</script>

<style scoped lang="less">
.bg{
  height: 250px;
  margin: 0 20px;
  padding-left: 50px;
  padding-top: 50px;
  .title{
    font-size: 20px;
  }
  input{
    margin-right: 10px;
  }
}
</style>

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/391709.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Serverless

Serverless&#xff1a;云计算的下一个十年 最近几年的技术圈&#xff0c;对于 Serverless 技术的讨论异常火热&#xff0c;在业内也有了很多成熟的案例&#xff0c;国外发展较早&#xff0c;比较有代表性的就是亚马逊和谷歌&#xff0c; 而在国内&#xff0c;腾讯和阿里两位巨…

Maven使用教程

1.什么是Maven&#xff1f; 当我们在创建一个使用Spring的Web项目就需要引入大量的jar包。一个项目Jar包的数量极多&#xff0c;并且Jar包之间的关系错综复杂&#xff0c;一个Jar包往往又会引用其他Jar包&#xff0c;缺少任何一个Jar包都会导致项目编译失败。 以往开发项目时…

GraphPad Prism v9.5.1.733 科研绘图软件多语言

GraphPad Prism集生物统计、曲线拟合和科技绘图于一体,其所具有的功能均非常实用和精炼,包括了一些特色的功能,如ROC曲线分析、Bland-Altman分析等;曲线拟合功能是GraphPad Prism8 汉化版超越其他统计软体的制胜法宝,GraphPad Prism8 汉化版的线性/非线性拟合功能使用操作…

JVM运行时数据区—Java虚拟机栈

虚拟机栈的背景 由于跨平台性的设计&#xff0c;java的指令都是根据栈来设计的。不同平台CPU架构不同&#xff0c;所以不能设计为基于寄存器的。 根据栈设计的优点是跨平台&#xff0c;指令集小&#xff0c;编译器容易实现&#xff0c;缺点是性能下降&#xff0c;实现同样的功…

python制作【法律条文查询工具】妈妈再也不担心我法盲了

前言 嗨喽~大家好呀&#xff0c;这里是魔王呐 ❤ ~! 更多教程源码资料电子书: 点击此处跳转文末名片获取 环境准备8 Python 3.8 Pycharm 《中华人民共和国刑法》 效果展示 打算做个简单的界面&#xff0c;主要功能就是查询法律条文 代码展示 查询器界面 设定界面大小 …

戴眼镜检测和识别2:Pytorch实现戴眼镜检测和识别(含戴眼镜数据集和训练代码)

Pytorch实现戴眼镜检测和识别(含戴眼镜数据集和训练代码) 目录 Pytorch实现戴眼镜检测和识别(含戴眼镜数据集和训练代码) 1.戴眼镜检测和识别方法 2.戴眼镜数据集 3.人脸检测模型 4.戴眼镜分类模型训练 &#xff08;1&#xff09;项目安装 &#xff08;2&#xff09;准…

反向代理和负载均衡有何区别?

反向代理和负载均衡是两种常用的网络架构模式&#xff0c;它们可以一起使用来提高网站的性能和可靠性&#xff0c;很多人会把这两者混淆&#xff0c;实际上&#xff0c;这两者的作用略有不同&#xff0c;今天我们就来详细说说具体区别是什么。一、反向代理&#xff08;Reverse …

Python|贪心|数组|桶排序|二叉搜索树|贪心|单选记录:最大间距|将有序数组转换为二叉搜索树|跳跃游戏 II

1、最大间距&#xff08;数组&#xff0c;桶排序&#xff09; 给定一个无序的数组&#xff0c;找出数组在排序之后&#xff0c;相邻元素之间最大的差值。 如果数组元素个数小于 2&#xff0c;则返回 0。 示例 1: 输入: [3,6,9,1] 输出: 3 解释: 排序后的数组是 [1,3,6,9]…

三个案例场景带你掌握Cisco交换机VLAN互通

VLAN间路由的方式现在主流的组网主要是依靠三层交换机通过配置SVI接口【有的厂商叫VLANIF接口】&#xff0c;当然也有比较小型的网络&#xff0c;它就一个出口路由器可管理的二层交换机&#xff0c;还有一种更加差的&#xff0c;就是出口路由一个可管理的二层交换机&#xff0c…

描述一下锁的四种状态及升级过程?

文章目录1、锁的四种状态2、Java对象头描述3、锁的升级过程&#xff08;Synchronized加锁/膨胀流程&#xff09;1&#xff09;简单过程如下图2&#xff09;详细过程&#xff08;1&#xff09;偏向锁&#xff08;2&#xff09;轻量级锁&#xff08;3&#xff09;重量级锁4、拓展…

SpringBoot下的Spring框架学习(Tedu)——DAY02

SpringBoot下的Spring框架学习&#xff08;Tedu&#xff09;——DAY02 目录SpringBoot下的Spring框架学习&#xff08;Tedu&#xff09;——DAY02Spring框架学习1.1 Spring介绍1.2 知识铺垫1.2.1 编辑Dog类1.2.2 编辑Cat类1.2.3 编辑测试类User.java1.2.4 上述代码的总结1.3 面…

PPQ库中KLD算法实现代码解析

PPQ量化工具库KLD算法解析前言PPQ算法实现NVIDIA的PPT中KLD算法流程KLD算法PPQ实现版本PPQ与NVIDIA的区别&#xff1a;前言 这是对PPQ库中KLD算法实现代码解析&#xff0c;关于PPQ库安装与使用详情见专栏上一篇博客。 PPQ算法实现 nvidia发布的PPT&#xff1a;8-bit Inferen…

使用vue-element-admin进行二次开发

vue-element-admin 介绍 基于 vue 和 element-ui实现 的后台前端解决方案 集成方案: vue-element-admin基础模板: vue-admin-template桌面终端: electron-vue-adminTypescript 版: vue-typescript-admin-template Others: awesome-project 亮点 i18 国际化解决方案 动态路由…

射频功率放大器在辉光放电特征及风速测量原理中的应用

实验名称&#xff1a;辉光放电特征及风速测量原理研究方向&#xff1a;辉光放电测试设备&#xff1a;信号发生器、ATA-8202射频功率放大器&#xff0c;热成像仪、万用表、等离子体传感器实验过程&#xff1a;在等离子体形成条件和流场响应机制的基础上&#xff0c;可以明确影响…

node使用支付宝沙箱模拟支付-好文

首页打开支付宝官方进行登录&#xff1a;支付宝开放平台 (alipay.com)下载支付宝提供的秘钥转化工具&#xff1a;密钥工具下载 - 支付宝文档中心 (alipay.com)打开工具生成密码&#xff1a;然后在支付宝页面配置秘钥开始配置nodenpm init -y初始化&#xff0c;再npm i express跟…

<Linux>进程间通信--管道

前言&#x1f603;&#x1f603;&#x1f603;进程间通信的方式管道 - Linux原生提供2SystemV - 多线程单机通信posix - 多线程网络通信这里我们主要是介绍一下管道一、生活和计算机中的管道&#x1f61c;生活中的管道特点都是有出口和入口的都是单向传输内容的(例如&#xff1…

雨水情测报设施包含哪些设备?

通常雨水情测报及大坝安全监测设施&#xff0c;主要包括&#xff1a;大坝安全监测终端、雨水情监测终端、GNSS监测站、雷达水位计、大坝安全监测平台、雨水情测报系统平台等产品。雨水情测报及大坝安全监测系统方案解决方案雨水情测报及大坝安全监测系统解决方案&#xff0c;主…

华为OD机试题,用 Java 解【字符串加密】问题

华为Od必看系列 华为OD机试 全流程解析+经验分享,题型分享,防作弊指南)华为od机试,独家整理 已参加机试人员的实战技巧华为od 2023 | 什么是华为od,od 薪资待遇,od机试题清单华为OD机试真题大全,用 Python 解华为机试题 | 机试宝典使用说明 参加华为od机试,一定要注意不…

Raft图文详解

Raft图文详解 refer to: Raft lecture (Raft user study) - YouTube Raft PDF Raft算法详解 - 知乎 (zhihu.com) 今天来详细介绍一下Raft协议 Raft是来解决公式问题的协议&#xff0c;那么什么是共识呢&#xff1f; 在分布式系统里面&#xff0c;consensus指的是多个节点对…

UEFI移植LVGL

自己组装过游戏主机的应该都有看到过&#xff0c;进入BIOS设置&#xff0c;酷炫的界面便呈现在眼前&#xff0c;而很多BIOS&#xff0c;使用的还是标准的界面。现在有个趋势&#xff0c;phoenix和insyde也在慢慢朝这种GUI界面发展&#xff0c;而AMI的使用C编写的界面已经非常完…