【微信小程序】数据绑定/绑定事件

313 阅读1分钟

一、数据绑定

1. data中初始化数据 2. 修改数据:this.setData

  data: {
    msg: '数据'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 修改msg的状态数据,语法:this.setData
    this.setData({
      msg: '修改后的数据'
    })
    // console.log(this.data.msg);
  },

二、绑定事件

1、bing绑定:事件绑定不会阻止冒泡事件向上冒泡

  <view class="goStudy" bindtap="handleParent">
    <text bindtap="handleChild">hello world</text>
  </view>
  handleParent() {
    console.log('parent');
  },
  handleChild() {
    console.log('child');
  },

点击子元素输出:image.png

2、catch绑定:事件绑定可以阻止冒泡事件向上冒泡

  <view class="goStudy" catchtap="handleParent">
    <text catchtap="handleChild">hello world</text>
  </view>
  handleParent() {
    console.log('parent');
  },
  handleChild() {
    console.log('child');
  },

点击子元素的输出:image.png

注意:事件回调放置的位置与data以及生命周期函数平级