WeChat Mini Program with parameters returns to the previous page

Return to the scenario encountered on the previous page with parameters:
User A purchases goods for settlement, enters the order page, and prompts that there is no delivery address for payment. User A triggers the add address event and jumps to the add address page. After entering the address information, click the save button to trigger the submit event.
I found related methods on the Internet, so I will simply make a record here.

The business logic for submitting an event is as follows:
1. Request the background API to add the newly received address data to the database, and the request status code returned by the background.
2. When the returned status code is 200, it indicates that the request was successful. In the successful callback, use the getCurrentPages () method to obtain the page stack, and assign the setData required for the previous page. Return to the previous page to carry the data.

formSubmit: function(e) {
  console.log(e)
  var that = this
  //发送请求
  wx.request({
    url: 'xxxx.com/api/Address/createAddr',
    header: { 'content-type': 'application/json' },
    method: 'POST',
    data: { ... 需提交的数据 },
    success: function(res) {
      //console.log(res)
      if (res.statusCode == 200) {
        wx.showToast({
            title: '提交成功',
        })
        
        //获取当前页面js里面的pages里的所有信息。
        let pages = getCurrentPages()
        console.log(pages)
        // -1是当前页面, -2是上一个页面,-3是上上个页面以此类推。
        let prevPage = pages[pages.length - 2]
        // 设置数据  是上一页你想被携带过去的数据,后面是本方法里你得到的数据
        prevPage.setData({
            address: e.detail.value.address,
            name: e.detail.value.name,
            phone: e.detail.value.phone
        })
        //prevPage.onShow()
        //返回上一页 这个时候数据就传回去了 可以在上一页的onShow方法里把传过去的数据输出来查看是否已经携带完成 
        wx.navigateBack({
            delta: 1
        })
      }
    },
    fail: function(res) {},
    complete: function(res) {}
  })
}  
Published 21 original articles · won praise 1 · views 7809

Guess you like

Origin blog.csdn.net/eva_feng/article/details/105065008