1、data传参
在使用 Axios 发送 HTTP 请求时,有三种常见的传参方式:data、params 和路径参数
1、data传参
this.$axios({ method: "post", url: "http://localhost:8080/api/user/login", data: { userId: this.userId, password: this.password, }, }) .then((res) => { console.log(res); }) .catch(error => { console.error(error); });
2、使用 params 传递查询参数:
axios.get('/api/users', { params: { page: 1, limit: 10 } }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });
params参数通常用于GET请求中添加查询参数,而对于POST请求,一般使用data参数来传递请求体数据。
3、使用路径参数传递数据:
deleteUser(userId) { this.$axios({ method: 'post', url: 'http://localhost:8080/api/user/deleteUser/' + userId, }).then((res) => { //显示删除成功 this.$message({ message: res.data.message, type: "success", }); //刷新表格数据 this.userAll(); });