axios put请求

1.3.3. 修改用户状态

  • 请求路径:users/:uId/state/:type
  • 请求方法:put
  • 请求参数
参数名 参数说明 备注
uId 用户 ID 不能为空携带在url中
type 用户状态 不能为空携带在url中,值为 true 或者 false
{
  "data": {
    "id": 566,
    "rid": 30,
    "username": "admin",
    "mobile": "123456",
    "email": "bb@itcast.com",
    "mg_state": 0
  },
  "meta": {
    "msg": "设置状态成功",
    "status": 200
  }
}

A.首先监听用户点击switch组件的事件,并将作用域插槽的数据当做事件参数进行传递

<!--html-->
<el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)"></el-switch>

B.在事件中发送请求完成状态的更改

async userStateChanged(row) {
  //发送请求进行状态修改
  const { data: res } = await this.$http.put(
    `users/${row.id}/state/${row.mg_state}`
  )
  //如果返回状态为异常状态则报错并返回
  if (res.meta.status !== 200) {
    row.mg_state = !row.mg_state
    return this.$message.error('修改状态失败')
  }
  this.$message.success('更新状态成功')
},
posted @ 2020-10-21 16:20  HF10  阅读(4687)  评论(0)    收藏  举报