使用封装的axios发送请求

使用封装的axios发送请求

1.src\api\api.js 定义请求路由方法:

import URLS from '../../config/urls'
    import { get, post } from './ajax'
    let base = URLS.API_URL
    // 用户相关
    export const postLogin = p => post(`${base}/login/`, p);
    export const getLogin = p => get(`${base}/login/`, p);

2.App.vue 导入路由请求方法测试请求数据:

    <template>
    <div id="app">
    <router-view/>
    </div>
    </template>

    <script>
    import { postLogin,getLogin } from './api/api'  // 导入路由请求方法
    export default {
    name: 'App',
    created () {
     this.testGet();
    this.testPost();
    },
    methods: {
      // 获取数据
      testPost() {
        // 调用api接口,并且提供了两个参数
        postLogin({
          type: 0,
          sort: 1,
          lst:[1,2,3,4,5]
        }).then(res => {
          // 获取数据成功后的其他操作
          console.log(res,1111111)
        }).catch(
        )
      },
  testGet() {
    // 调用api接口,并且提供了两个参数
    getLogin({
      type: 0,
      sort: 1,
      lst:[1,2,3,4,5]
    }).then(res => {
          // 获取数据成功后的其他操作
          console.log(res,22222222222222)
          }).catch(
        )
      },
    }
}
</script>

 

posted @ 2020-09-28 20:20  做你的猫231  阅读(433)  评论(0)    收藏  举报