其实我们主要是想实现点击取消的时候直接隐藏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
    })
  },

 

相关文章:

  • 2021-04-07
  • 2021-12-01
猜你喜欢
  • 2021-08-01
  • 2021-05-01
  • 2022-01-06
  • 2021-09-02
  • 2021-10-22
相关资源
相似解决方案