vue-js

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

2019-05-10  本文已影响0人  xiaojianxu

以下代码所示,需要在 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().

上一篇下一篇

猜你喜欢

热点阅读