el-switch的使用
基础使用参考官网
这篇文章记录更改用户状态,如果操作失败,回到操作前的状态的方法
change事件监听用户操作
代码
<template> <el-table :data="tabelData" border> <el-table-column prop="id" label="编号"></el-table-column> <el-table-column prop="username" label="姓名"></el-table-column> <el-table-column label="状态"> <template slot-scope="scope"> <!-- {{scope.row}} --> <el-switch v-model="scope.row.status" @change="changeUserStatus(scope.row)"></el-switch> </template> </el-table-column> </el-table> </div> </template> <script> export default{ name:"Custom", data(){ return{ tabelData:[ {username:"Ann",id:1,status:true}, {username:"Linda",id:2,status:false}, {username:"July",id:3,status:true} ] } }, methods:{ async changeUserStatus(userinfo){ console.log(userinfo) await let {data:res}=this.$http.put(`/update/${userinfo.id}/status/${userinfo.status}`)//使用了es6中的对象解析和字符串模板 } if(res.code!=200){ userinfo.status=!userinfo.status; } } } </script>