编写小的demo应用axios异步请求.

效果图示:



功能: 用户在输入框中输入信息进行搜索,并搜索状态随之改变(四种状态).

项目目录:



代码:

1.index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>vue_demo</title>
<link rel="stylesheet" href="./static/css/bootstrap.css">
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

2.main.js

import Vue from 'vue'
import App from './App' new Vue({
el: '#app',
components: { App },
template: '<App/>'
})

3.App.vue

<template>
<div class="container">
<Search/>
<users-main/>
</div>
</template> <script>
import Search from './components/Search'
import Main from './components/Main' export default { data () {
return { }
}, components: {
Search,
UsersMain: Main
} }
</script> <style> </style>

4.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="searchName"/>
<button @click="search">Search</button>
</div>
</section>
</template> <script>
import PubSub from 'pubsub-js' export default {
data() {
return {
searchName: ''
}
},
methods: {
search() {
// 获取输入信息
const searchName = this.searchName.trim()
// console.log(searchName)
if (searchName){ // 当不为空字符串时
PubSub.publish('searchInfo', searchName) // 发布搜索的消息:消息名和数据
}
}
}
}
</script> <style> </style>

5.Main.vue

<template>
<div>
<h2 v-if="firstView">请输入用户名进行搜素</h2>
<h2 v-if="loading">Loading...</h2>
<h2 v-if="errorMsg">{{errorMsg}}</h2>
<div class="row">
<div class="card" v-for="(user,index) in users" :key="index">
<a :href="user.url" target="_blank">
<img :src="user.avatar_url" style='width: 100px'/>
</a>
<p class="card-text">{{user.name}}</p>
</div> </div>
</div>
</template> <script>
import PubSub from 'pubsub-js'
import axios from 'axios' export default {
data() {
return {
firstView: true,
loading: false,
users: null, // [{url: '', avatar_url: '', name: ''}]
errorMsg: ''
}
},
mounted() {
// 订阅搜索的消息,绑定监听; 在点击search按钮后发送ajax请求
PubSub.subscribe('searchInfo',(msg, searchName) =>{
// 定义url
const url = ` https://api.github.com/search/users?q=${searchName}` // 更新状态(请求中)
this.firstView = false
this.loading = true
this.users = null // 这里需重新设置为null,不然加载时会显示上一次users信息
this.errorMsg = '' // 发送ajax请求
axios.get(url).then(response => {
// 请求成功
const result = response.data
const users = result.items.map(item => ({
url: item.html_url,
avatar_url: item.avatar_url,
name: item.login
})) // 更新状态(成功)
this.loading = false
this.users = users }).catch(error => {
// 请求失败(失败)
this.loading = false
this.errorMsg = '请求失败' })
})
}
}
</script> <style>
.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>

vue axios应用的更多相关文章

  1. vue axios 取消上次请求

    axios.defaults.timeout = 1000 * 5axios.defaults.baseURL = baseUrlvar CancelToken = axios.CancelToken ...

  2. vue axios拦截器 + 自编写插件 实现全局 loading 效果;

    项目需求:用自定义的 .gif 图标实现全局 loading 效果:为避免在每个页面手动添加,且简单高效的实现,经查阅资料,最终采用了 vue axios拦截器 + 自编写 loading 插件:下面 ...

  3. vue axios使用form-data的形式提交数据的问题

    vue axios使用form-data的形式提交数据vue axios request payload form data由于axios默认发送数据时,数据格式是Request Payload,而并 ...

  4. VUE AXIOS 跨域问题

    背景: 后台跨域使用通配符:context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); ...

  5. vue axios 总结篇

    1.npm --save 和 --save-dev 有什么区别 发布到线上的叫生产环境~,在本地开发的时候叫开发环境,--save就是会打包到线上去并且在线上环境能用到的,比如你npm install ...

  6. 基于Vue + axios + WebApi + NPOI导出Excel文件

    一.前言 项目中前端采用的Element UI 框架, 远程数据请求,使用的是axios,后端接口框架采用的asp.net webapi,数据导出成Excel采用NPOI组件.其业务场景,主要是列表页 ...

  7. vue+axios新手实践实现登陆

    vue+axios新手实践实现登陆 https://segmentfault.com/a/1190000015201803 增加 利用HTML5的history.replacestate()修改当前页 ...

  8. vue+axios+elementUI文件上传与下载

    vue+axios+elementUI文件上传与下载 Simple_Learn 关注  0.5 2018.05.30 10:20 字数 209 阅读 15111评论 4喜欢 6 1.文件上传 这里主要 ...

  9. axios interceptors 拦截 , 页面跳转, token 验证 Vue+axios实现登陆拦截,axios封装(报错,鉴权,跳转,拦截,提示)

    Vue+axios实现登陆拦截,axios封装(报错,鉴权,跳转,拦截,提示) :https://blog.csdn.net/H1069495874/article/details/80057107 ...

  10. Vue+axios(interceptors) 实现http拦截 + router路由拦截 (双拦截)+ 请求自带loading效果

    axios interceptors 拦截器 //interceptors.js // vue axios配置 发起请求加载loading请求结束关闭loading // http request 请 ...

随机推荐

  1. print和赋值

    赋值 #可同时(并行)给多个变量赋值 x, y, z = 1, 2, 3 #交换多个变量的值 x, y = y, x 序列解包(或可迭代对象解包):将一个序列(或任何可迭代对象)解包,并将得到的值存储 ...

  2. Codeforces 919E Congruence Equation ( 数论 && 费马小定理 )

    题意 : 给出数 x (1 ≤ x ≤ 10^12 ),要求求出所有满足 1 ≤ n ≤ x 的 n 有多少个是满足 n*a^n  = b ( mod p ) 分析 : 首先 x 的范围太大了,所以使 ...

  3. 2018百度之星B轮 degree

    degree Accepts: 1581 Submissions: 3494 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/1 ...

  4. luogu P1077 摆花 x

    P1077 摆花 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能 ...

  5. Cloud Computing——Everything as a Service

    service 分类 有Iaas, Paas, SaaS HDFS 总结☞: HDFS应付不了的场景 无法低时延 小文件存储存在空间利用率问题 文件不可修改 三副本有什么作用 防止单机故障,提高可用性 ...

  6. HDU 6191 Query on A Tree(字典树+离线)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  7. HDU 6012 Lotus and Horticulture(离散化)

    题目代号:HDU 6012 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6012 Lotus and Horticulture Time Limit: ...

  8. go语言系列--前言

    我为什么要学golang语言 绝不是一时兴起,也不是人云亦云,这是我规划了很久的事了. 我曾自学过C语言,C++语言,Python语言,可都学的不精,原因我想是不知道为了什么而学的,可是这就是缺少学习 ...

  9. Nginx的正则表达式

    Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Ра ...

  10. D2. Equalizing by Division (hard version)

    D2. Equalizing by Division (hard version) 涉及下标运算一定要注意下标是否越界!!! 思路,暴力判断以每个数字为到达态最小花费 #include<bits ...