美文网首页vue-js
#2-wx.functionName 回调函数,如何调用自定义方

#2-wx.functionName 回调函数,如何调用自定义方

作者: xiaojianxu | 来源:发表于2019-05-10 12:16 被阅读0次

以下代码所示,需要在 wx.showModal 的 success 中调用 methods 的goBack。

直接使用 this 是不行的,在 success 回调函数中传参数,如: function (res, this) 报错。

<script>
export default {
  data () {
    return {
      modalVisible: false,
      categories: [],
      selectedCatesId: [],
      realSelectedCateId: {}
    }
  },
  onLoad () {
    this.getCategories()
  },
  methods: {
    onChange () {

    },
    async getCategories () {
      let url = 'https://weapp.es668.cn:55559/sort/policy-more'
      let res = await this.$http.get({url: url, data: {}})
      this.categories = res.data
      console.log(res)
    },
    changeCurrent (key, i, type, id) {
      this.selectedCatesId.splice(key, 1, i)
      this.realSelectedCateId[type] = id
      console.log(this.selectedCatesId)
      console.log(this.realSelectedCateId)
    },
    confirm () {
      if (Object.keys(this.realSelectedCateId).length === 1) {
        var that = this
        wx.showModal({
          title: '提示',
          content: '你未选择任一筛选条件',
          success: function (res) {
            if (res.confirm) {
              // this.goBack()
              // return res.confirm
              that.goBack()
            } else if (res.cancel) {
              return res.cancel
            }
          }
        })
      }
    },
    goBack () {
      console.log(this.realSelectedCateId)
      let url = '/pages/policy/apply/main?' + this.realSelectedCateId
      wx.navigateTo({url: url})
    }
  }
}
</script>

解决方法是:

var that = this
回调函数中, 使用 that 调用函数,that.goBack().

相关文章

  • #2-wx.functionName 回调函数,如何调用自定义方

    以下代码所示,需要在 wx.showModal 的 success 中调用 methods 的goBack。 直接...

  • python之回调函数和装饰函数

    一.回调函数 1.回调函数的概念: 是在某一函数中调用另一个函数变量方式,来执行函数.回调函数不是有实现方调用,...

  • Promise

    回调: 1.把一个函数A传给另一个函数B调用,那么A就是回调函数。 function B(){ A() //调用方...

  • Java回调

    回调示例代码 被调用方 Reponse::handle 回调接口(函数式接口) 请求方 打印结果

  • 10 泛型库

    回调 回调的含义是:对一个库,用户希望库能够调用用户自定义的某些函数,这种调用称为回调。C++中用于回调的类型统称...

  • 不懂C语言回调函数,那就看这篇文章吧!

    什么是回调函数 我们先来看看百度百科是如何定义回调函数的: 回调函数就是一个通过函数指针调用的函数。如果你把函数的...

  • 异步问题

    什么是回调地狱(函数作为参数层层嵌套)回调函数(一个函数作为参数需要依赖另一个函数执行调用)如何解决回调地狱 pr...

  • WebHooks、WebSocket 、SSE(Server-s

    WebHooks 定义 ​ Webhook一个web自定义回调函数,当程序发生警报行为时,会自动回调调用...

  • callback function(回调函数)

    简单的说,我们调用别人的API叫call,调用的第三方api调用我们的函数叫回调(callback) 回调机制 比...

  • 回调函数和array_map()函数

    回调函数:某个函数通过利用指针来重复调用某个函数。被调用的函数成为回调函数。 array_map( [函数名字],...

网友评论

    本文标题:#2-wx.functionName 回调函数,如何调用自定义方

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