vue+ant调用后端数据 控制switch开关

2023.8发现自己之前的一个草稿,稚嫩的功能。想想决定整理后发出来,当做纪念。

思路:

1.调用后端接口接收数据status

2.每列status判断开关switch的开关状态

3.每次开关,将id对应的开关状态status返回后端

Html:

        <a-row class="pushSettings">
          <a-col :style="{ marginBottom: '8px' }" :span="24">
            <div class="pushswitch" v-for="(item, index) in switchList">
              <div class="pushContent">{{ item.pushContent }}</div>
              <div class="pushJudgment">
                <a-switch
                  :default-checked="item.ifPush == 0 ? true : false"
                  @change="judgmentSwitch(item, index)"
                />
              </div>
            </div>
          </a-col>
        </a-row>

后端接口:

从/pull获取到
 {
            "ifPush": 0,1 开/关
            "id": 1, 内容的id
            "pushContent": "内容1"
 },

点击开/关按钮 调用/push接口
传上面接口获取到的id  字段名pushId
状态status  点开就传0  关就传1

CSS:

.pushSettings {
  width: 100%;
  height: 100%;
  background: #ffffff;
  border-radius: 16px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  .pushContent {
    padding-right: 24px;
    font-size: 16px;
    font-weight: bold;
  }
  .pushswitch {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
  }
}

1.调用后端接口读取 id和 status

data() {
    return {
      pushId: '',
      ifPush: '',
      switchList: {},
    }
},

created() {
  this.getPush()
},

methods: {
   getPush() {
     let that = this
     postAction('/pull', {}).then((res) => {
        that.switchList = res.result
      })
   },

   judgmentSwitch(item,index) {
    let params = {
     pushId:this.pushId,
     ifPush:this.ifPush,
     }
     postAction('/pull', {params}).then((res) => {
       that.switchList = res.result
     })
   }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值