vue的学习之路(Axios 基本使用)

<script src="js/axios.min.js"></script>

<>3、在页面中引用

<>4、发送异步请求

  • url?xxx

  • rest请求【GET,POST,DELETE,PATCH

<>5、准备一个SpringBoot的控制层与axios进行交互

  • 实体类

public class User {

private Integer id;

private String username;

  • 控制层

/**

  • @author 王恒杰

  • @date 2021/12/16 16:25

  • @Description:

*/

@Controller

@CrossOrigin //设置当前控制器支持所有域访问 解决跨域问题

public class UserAction {

@RequestMapping("test1")

@ResponseBody

public String test1(Integer id,String username){

System.out.println("test1");

System.out.println("id:"+id);

System.out.println("username:"+username);

return "test1 response"+id+username;

}

}

<>6、 GET方式的请求

axios.get("http://localhost:8080/aa/test1?id=1\&username=whj")

.then(function (response) {

console.log(response.data)

})

.catch(function (err) {

console.log(err)

})

<>7、POST方式请求

  • 前台传的数据是json

//发送POST方式请求

axios.post("http://localhost:8080/aa/test2",{

id:2,

username:"王恒杰"

}).then(function (response) {

console.log(response.data);

}).catch(function (err) {

console.log(err)

})

  • **注意: post接收数据时,需要在参数上添加 ** requestBody

<>8. axios并发请求

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

function getUserAccount() {

return axios.get('/user/12345');

}

function getUserPermissions() {

return axios.get('/user/12345/permissions');

}

axios.all([getUserAccount(), getUserPermissions()])

.then(axios.spread(function (acct, perms) {

// 两个请求现在都执行完成

}));

<>9.自定义配置对象发送请求

//自定义配置对象发送请求(创建实例 发送异步请求) 【企业开发必背】

var instaus = axios.create({

baseURL:"http://localhost:8080/aa/",

// timeout: 5000 //设置响应超时时间 如果超过了5000秒报异常

});

instaus.get("test1?id=3&username=yfj")

.then(function (response) {

console.log(response.data);

})

.catch(function (err) {

console.log(err);

})

<>10、 拦截器

  • 作用:抽取共有代码 解决冗余问题,提升效率,提高程序的可维护性

axios中的拦截器:解决冗余问题

请求拦截器:发送请求时可以进行拦截,添加功能

响应拦截器:响应回来时进行拦截 添加功能

var instaus = axios.create({

baseURL:"http://localhost:8080/aa/",

// timeout: 5000 //设置响应超时时间 如果超过了5000秒报异常

});

//添加请求拦截器

instaus.interceptors.request.use(function (config) {

总结

为了帮助大家更好温习重点知识、更高效的准备面试,特别整理了《前端工程师面试手册》电子稿文件。

内容包括html,css,JavaScript,ES6,计算机网络,浏览器,工程化,模块化,Node.js,框架,数据结构,性能优化,项目等等。

包含了腾讯、字节跳动、小米、阿里、滴滴、美团、58、拼多多、360、新浪、搜狐等一线互联网公司面试被问到的题目,涵盖了初中级前端技术点。

前端面试题汇总

JavaScript

性能

linux

相关推荐
工业互联网专业28 分钟前
毕业设计选题:基于ssm+vue+uniapp的校园水电费管理小程序
vue.js·小程序·uni-app·毕业设计·ssm·源码·课程设计
豆豆42 分钟前
为什么用PageAdmin CMS建设网站?
服务器·开发语言·前端·php·软件构建
计算机学姐1 小时前
基于SpringBoot+Vue的在线投票系统
java·vue.js·spring boot·后端·学习·intellij-idea·mybatis
彤银浦1 小时前
python学习记录7
python·学习
少女忧1 小时前
51单片机学习第六课---B站UP主江协科技
科技·学习·51单片机
twins35202 小时前
解决Vue应用中遇到路由刷新后出现 404 错误
前端·javascript·vue.js
qiyi.sky2 小时前
JavaWeb——Vue组件库Element(3/6):常见组件:Dialog对话框、Form表单(介绍、使用、实际效果)
前端·javascript·vue.js
煸橙干儿~~2 小时前
分析JS Crash(进程崩溃)
java·前端·javascript
安冬的码畜日常2 小时前
【D3.js in Action 3 精译_027】3.4 让 D3 数据适应屏幕(下)—— D3 分段比例尺的用法
前端·javascript·信息可视化·数据可视化·d3.js·d3比例尺·分段比例尺
邓校长的编程课堂2 小时前
助力信息学奥赛-VisuAlgo:提升编程与算法学习的可视化工具
学习·算法