接口定义

接口参数和返回值需要定义为强类型,特殊情况可以用any

  1. import { createAPI } from '@/utils/request'
  2. // 用户列表
  3. export function list(params: any): Promise<any> {
  4. return createAPI('/users', 'get', params)
  5. }
  6. // 用户添加
  7. export function add(body: any): Promise<any> {
  8. return createAPI('/users', 'post', null, body)
  9. }
  10. // 用户修改
  11. export function edit(id: number, body: any): Promise<any> {
  12. return createAPI(`/users/${id}`, 'put', null, body)
  13. }
  14. // 用户删除
  15. export function remove(id: number): Promise<any> {
  16. return createAPI(`/users/${id}`, 'delete')
  17. }
  18. // 用户详情
  19. export function detail(id: number): Promise<any> {
  20. return createAPI(`/users/${id}`, 'get')
  21. }

组件中读取数据

  1. // 1 导入接口包
  2. import { list, remove } from '@/api/users'
  3. // 2 定义接收对象
  4. private listData: Page<User> = {
  5. counts: 0,
  6. page: 0,
  7. items: [],
  8. pages: 0,
  9. pagesize: 0
  10. }
  11. // 3 采用 promise 方式读取
  12. private async getList() {
  13. const { data } = await list()
  14. this.listData = data
  15. }

request 参考

采用 axios 二次封装,更多处理你可以在 interceptors 中实现。

  1. import axios, { AxiosRequestConfig, Method } from 'axios'
  2. import { Message /*, MessageBox*/ } from 'element-ui'
  3. import { getToken } from '@/utils/cookies'
  4. const service = axios.create({
  5. baseURL: '/api',
  6. timeout: 5000 * 5
  7. })
  8. service.defaults.headers.post['Content-Type'] = 'application/json'
  9. // Request interceptors
  10. service.interceptors.request.use(
  11. config => {
  12. const token = getToken()
  13. if (token) {
  14. config.headers['Authorization'] = token
  15. }
  16. return config
  17. },
  18. error => {
  19. Promise.reject(error)
  20. }
  21. )
  22. // Response interceptors
  23. service.interceptors.response.use(
  24. response => response,
  25. error => {
  26. Message({
  27. message: error.response.data.errMessage || error.message,
  28. type: 'error',
  29. duration: 5 * 1000
  30. })
  31. return Promise.reject(error)
  32. }
  33. )
  34. export const createAPI = (
  35. url: string,
  36. method: Method = 'get',
  37. params: any = undefined,
  38. data: any = undefined
  39. ) => {
  40. const config: AxiosRequestConfig = {}
  41. config.url = url
  42. config.method = method
  43. if (params !== undefined) {
  44. config.params = params
  45. }
  46. if (method != 'get' && data !== undefined) {
  47. config.data = data
  48. }
  49. return service(config)
  50. }
  51. export const createFormAPI = (url: string, method: Method, data: any) => {
  52. const config: AxiosRequestConfig = {}
  53. config.url = url
  54. config.method = method
  55. config.data = data
  56. config.headers = {
  57. 'Cache-Control': 'no-cache',
  58. // 'Content-Type': 'application/x-www-form-urlencoded'
  59. 'Content-Type': 'multipart/form-data'
  60. }
  61. // config.responseType = 'json'
  62. // config.transformRequest = [
  63. // function(data) {
  64. // let ret = ''
  65. // for (let it in data) {
  66. // ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  67. // }
  68. // return ret
  69. // }
  70. // ]
  71. return service(config)
  72. }
  73. export default service