Vue—Axios 基本使用

阅读: 评论:0

Vue—Axios 基本使用

Vue—Axios 基本使用

Axios 基本使用

引言

Axios 是一个异步请求技术,核心作用就是用来在页面中发送异步请求,并获取对应数据在页面中渲染 页面局部更新技术 Ajax

Axios 第一个程序

中文网站:

安装: .min.js

下载:

npm install axios --save

GET方式的请求

//发送GET方式请求("localhost:8989/user/findAll?name=xiaochen").then(function(response){console.log(response.data);}).catch(function(err){console.log(err);});

POST方式请求

//发送POST方式请求axios.post("localhost:8989/user/save",{username:"xiaochen",age:23,email:"xiaochen@zparkhr",phone:13260426185}).then(function(response){console.log(response.data);}).catch(function(err){console.log(err);});

axios并发请求

并发请求: 将多个请求在同一时刻发送到后端服务接口,最后在集中处理每个请求的响应结果

//1.创建一个查询所有请求function findAll(){("localhost:8989/user/findAll?name=xiaochen");}//2.创建一个保存的请求function save(){return axios.post("localhost:8989/user/save",{username:"xiaochen",age:23,email:"xiaochen@zparkhr",phone:13260426185});}//3.并发执行axios.all([findAll(),save()]).then(axios.spread(function(res1,res2){  //用来将一组函数的响应结果汇总处理console.log(res1.data);console.log(res2.data);}));//用来发送一组并发请求

案例

<div id="app"><input type="text"  v-model="name" @keyup.delete="shows" &#="searchCity"/> <input type="button" value="搜索" @click="searchCity"/> <br><span v-for="city in hotCitys"><a href="" @click.prevent="searchCitys(city)">{{ city }}</a> &nbsp;</span><hr><span v-show="isShow">{{ name }},今天的天气是: {{ message }} </span></div>
<!--引入vue-->
<script src=".js"></script>
<!--引入axios-->
<script src=".min.js"></script>
<script>const app = new Vue({el: "#app",data: {name:"",hotCitys:["北京","上海","广州","深圳"],message:"",isShow:false,},methods: {searchCity(){//获取输入的城市信息let _this = this;//发送axios请求("localhost:8989/city/find?name="+this.name).then(function (response) {console.log(ssage);_ssage = ssage;_this.isShow = true;}).catch(function (err) {console.log(err);});},searchCitys(name){this.name = name;this.searchCity();//函数中调用函数},shows(){this.isShow = false;}}});
</script>

本文发布于:2024-01-30 16:20:27,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170660282921289.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Vue   Axios
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23