uni-app使用vant ui开关接口

uni-app使用vant ui Switch 开关接口,点击开的时候status传1给接口,关的时候传0,应该怎么改啊?


<van-switch @change="changeSwitch(row)" v-model="row.status" size="23px" active-value="1" inactive-value="0" />

data() {
            return {
                checked: true,
                formRecord:{
                    status: '1'
                },
                row:[]
            }
        },


async changeSwitch(row){
                this.formRecord.status = row.status
                const {
                    status
                } = this.formRecord;
                const res = await this.req(api.openOrCloseMobilePhone({ status }));
                if (!res) return;
                this.row.status = res.data
            },

源于chatGPT仅供参考


您可以按照以下方式修改代码以实现您的需求:

```html
<van-switch @change="changeSwitch(row)" v-model="row.status.toString()" size="23px" active-value="1" inactive-value="0" />

v-model绑定的值row.status转换为字符串形式,这样在切换开关状态时会传递字符串类型的值。

data() {
  return {
    checked: true,
    formRecord: {
      status: '1'
    },
    row: {}
  }
},
async changeSwitch(row) {
  this.formRecord.status = row.status;
  const res = await this.req(api.openOrCloseMobilePhone({ status: row.status }));
  if (!res) return;
  this.row.status = res.data;
}

changeSwitch方法中,直接使用row.status作为参数传递给请求接口,并更新this.row.status的值来保持同步。

这样,在点击开关时,状态为开时会传递字符串'1'给接口,状态为关时会传递字符串'0'给接口。

```