使用el-switch實現開關(更改狀態後展示狀態)
阿新 • • 發佈:2020-08-24
今天做了一個使用者許可權的功能,裡面用到了開關,
程式碼實現:
頁面:
<el-switch v-model="scope.row.status" @change="disable(scope.row)"> </el-switch>
js:
disable(row) { const { status, userId } = row let params = { userId: userId, status: status ? '1' : '0' // 三元運算子 類似於if else }; updateStatus(params).then(res => { if (res.code === 0) { this.$message.success(`${status ? '已啟用' : '已停用'}`) this.queryList(); } }); },
OK,寫到這步就已經實現了,前端向後臺傳入狀態值,那怎麼後端返回來的狀態值怎麼展示到頁面上呢 ?
看下面程式碼:
queryAllUser(params).then(res => { if (res.code === 0) { const arr = res.data.records;// 返回來的值 arr.forEach(item => { // 迴圈一下 if (item.status === '0') { item.status = false //賦值 } else { item.status = true } }) this.tableData = arr } });
over~~~