今天做了一个用户权限的功能,里面用到了开关,

使用el-switch实现开关(更改状态后展示状态)

 

 

 代码实现:

页面:

<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~~~

相关文章:

  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-30
  • 2022-12-23
  • 2022-01-01
  • 2022-01-13
  • 2021-04-29
  • 2022-12-23
相关资源
相似解决方案