Last active
April 12, 2019 08:41
-
-
Save actior/9564c9aeca47b2968f273e39a1d0a8c0 to your computer and use it in GitHub Desktop.
知识点补充
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
今天讲两篇,此为上篇,下篇会接着! | |
截止目前,我们的物业系统越来越完善,但是还是很大一部分还是没有完成,包括测试之类的。 | |
话不多说,先讲讲API接口篇,本来接口没什么好讲的,但是不一定,像我这种新手就掉坑里了。先了解axios请求,从官方文档来看,axios是基于promise的HTTP库,可以用在浏览器和node.js中。 | |
其特点是: | |
``` | |
从浏览器中创建 XMLHttpRequests | |
从 node.js 创建 http 请求 | |
支持 Promise API | |
拦截请求和响应 | |
转换请求数据和响应数据 | |
取消请求 | |
自动转换 JSON 数据 | |
客户端支持防御 XSRF | |
``` | |
它目前都支持主流的浏览器内核,不知道支持不支持ie6/7,可能不支持吧,毕竟,现在大多数ie浏览器也逐渐换代了,这点倒不用我们来担心。 | |
我是一般是通过npm来进行安装的,其安装教程就是这样 | |
安装node.js-在开始输入运行,输入cmd后检查是否安装好node.js,检查的方法自己百度,我这就不一一展示了(我也忘记了)-之后输入npm install axios --save ,我一般是在编辑器下安装的,我这里比较推荐使用vscode,很多人说它不好,我觉得他们都是键盘侠,每个人都有喜好,各人安定。 | |
来说说的请求方式分为get,post后面会讲到put,delete这种比较少见的请求方式, | |
get请求: | |
``` | |
// 为给定 ID 的 user 创建请求 | |
axios.get('/user?ID=12345') | |
.then(function (response) { | |
console.log(response); | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}); | |
// 可选地,上面的请求可以这样做 | |
axios.get('/user', { | |
params: { | |
ID: 12345 | |
} | |
}) | |
.then(function (response) { | |
console.log(response); | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}); | |
``` | |
一般我们开发是这样写的 | |
``` | |
import axios from 'axios' | |
// 查询管理员列表 | |
export function area(params){ | |
let url='/property/area' | |
return new Promise((resolve, reject) => { | |
axios.get(url, {params:params}).then(res => { | |
resolve(res.data) | |
}).catch(err => { | |
reject(err) | |
}) | |
}) | |
} | |
``` | |
也可以这样写 | |
``` | |
import axios from 'axios' | |
//缴费标准类 | |
class payStandard { | |
queryStanBindInfo(params) { | |
let url = '/property/query_standard_info' | |
return new Promise((resolve, reject) => { | |
axios.get(url, { | |
params: params | |
}).then(res => { | |
resolve(res.data) | |
}).catch(err => { | |
reject(err) | |
}) | |
}) | |
} | |
} | |
export { | |
payStandard | |
} | |
``` | |
两种其实差不多,功能都是一样的,最后一种是在页面写接口的时候导入import 只写一个函数,只要在请求的时候这样写 | |
``` | |
let getRoom = new payStandard(); | |
getRoom | |
.queryStanBindInfo({ | |
id: id | |
}) | |
``` | |
而post请求: | |
``` | |
axios.post('/user', { | |
firstName: 'Fred', | |
lastName: 'Flintstone' | |
}) | |
.then(function (response) { | |
console.log(response); | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}); | |
``` | |
请看我开发时写的 | |
``` | |
export function signupOne(params) { | |
let url = "/property/register_admin" | |
return new Promise((resolve, reject)=>{ | |
axios.post(url, params).then(res=>{ | |
resolve(res.data) | |
}).catch(err=>{ | |
reject(err) | |
}) | |
}) | |
} | |
``` | |
至于多并发请求,实际我用不多! | |
而put | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment