当使用重定向方法wx.redirectTo(OBJECT)或关闭当前页返回上一页wx.navigateBack(),触发onUnload
例如:
我们定义了一个方法,功能是:当用户打开这个页面就会调用下面这个函数
01 | /** |
02 | * 获取商品详情信息 |
03 | */ |
04 | getdata: function (product_id) { //定义函数名称 |
05 | var that = this ; |
06 | wx.request({ |
07 | url: url, //请求地址 |
08 | header: { //请求头 |
09 | "Content-Type" : "applciation/json" |
10 | }, |
11 | method: "GET" , //get为默认方法/POST |
12 | success: function (res) { |
13 | that.setData ({ |
14 | data: res.data |
15 | }, |
16 | fail: function (err) { }, //请求失败 |
17 | complete: function () { } //请求完成后执行的函数 |
18 | }) |
19 | }, |
1 | /** |
2 | * 生命周期函数--监听页面加载 |
3 | * options是接收参数,如 ?id=123,接收这个id,就用options.id |
4 | */ |
5 | onLoad(options) { |
6 | this .getdata(options.id); |
7 | }, |