示例代码:
// 封装请求函数
const request = (url, params) => {
return new Promise((resolve, reject) => {
// ...do something
})
}
// 使用时
const handleLogin = () => {
request(
'/basic/login',
{
usename: 'sunshine',
password: '123456'
}
).then(res => {
// success do something
}).catch(err => {
// fail do something
})
}
上面的代码使用async/await的话可以改写成:
const handleLogin = async () => {
const res = await request('/basic/login', {
usename: 'sunshine',
password: '123456'
})
const info = await request('/basic/getuserinfo', {
id: res.id
})
this.userInfo = info
}
网友评论