美文网首页
微信小程序 - 子组件中的properties和data操作起来

微信小程序 - 子组件中的properties和data操作起来

作者: coderfl | 来源:发表于2020-03-26 16:34 被阅读0次

一组复选框实现双向绑定

<block wx:for="{{info}}" wx:key="index">
  <van-checkbox value="{{ item.my_checked }}" bind:change="onChange" data-id="{{item.id}}"/>
</block>
/////
Component({
    properties: {
      info:{
        type:Array,
        value:[]
      }
    },
    data: {},
    methods: {
      onChange(event) {
        const id = event.currentTarget.dataset.id;
        const index = this.data.info.findIndex(value => value.id === id);
        // 小程序没有vue的v-model,手动实现
        const info = this.data.info;
        info[index].my_checked = event.detail;
        this.setData({info})
      }
    }
});

相关文章

网友评论

      本文标题:微信小程序 - 子组件中的properties和data操作起来

      本文链接:https://www.haomeiwen.com/subject/grvcuhtx.html