axios使用post和get发起请求

安装

yarn add axios

导入项目
修改main.js

import axios from 'axios'
Vue.prototype.$http = axios.create({
  baseURL:'http://localhost:3000'
})

解决Property '$http' does not exist on type 'xxx'
在项目根目录下新建axios.d.ts

import Vue from 'vue'
import { AxiosInstance } from 'axios';
declare module 'vue/types/vue' {
  interface Vue {
    $http: AxiosInstance
  }
}
post

前台

//数据用js对象表示
let data = {name:"john",age:17};
this.$http.post(`${this.resource}/create`,data)
.then(res=>{
    console.log('res=>',res);            
})

服务端

async create(@Body() dto: Dto) {
      global.console.log(dto.name)
}
get

前台

this.$http.post.get(`${this.resource}/create`, {
        params: {
          name:"john",
          age:17
        }
});

服务端

async create(@Query('name') name: string, @Query('age') age: int = 0) {
...
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容