安装
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) {
...
}