微信小程序自定义Searchbar取消事件

 

其实我们主要是想实现点击取消的时候直接隐藏Searchbar,但目前的取消是仅把input框重置了。

不知道为啥微信小程序的Searchbar没有允许直接bindcancel事件,在微信社区找到答案需要自己重写后才行。

 

  /** searchBar 部分 */
  reSetSearchbarCancel() {
    let sbar = this.selectComponent("#或.你的searchbar"), { hideInput } = sbar;
    // 重写
    Object.defineProperties(sbar.__proto__, {
      hideInput: {
        configurable: true,
        enumerable: true,
        writable: true,
        value(...p) {
          // 加上这句,同时wxml需要加上bindcancel="cancel"
          this.triggerEvent('cancelSearchbar', {})
          // 或者这里直接调用下面的cancel方法,那么wxml就不需要bindcancel
          // t.cancel()
          // 执行原方法,返回原方法结果
          return hideInput.apply(sbar, p)
        }
      }
    })
  },
  cancelSearchbar() {
   // 执行你的操作
    this.setData({
      show_minzu: false
    })
  },

 

posted @ 2021-09-23 11:00  一 定 会 去 旅 行  阅读(399)  评论(0编辑  收藏  举报