The applet uses two-way data binding, just to find a way to update the data you want to update
1.onload method is called during page initialization
Method call sequence experience: first investigate the initialization function and call token detection without parameter initialization. The following are the calls of two different onload methods
/** * Creative contest homepage loading - no initialization parameters */ onLoad: function(options) { const _this = this const token = wx.getStorageSync('token'); console.log(token) if (!token) { _this.goLoginPageTimeOut() return } /** * token Inspection **/ WXAPI.checkToken(token).then(function (res) { if (res.code != 1000) { wx.removeStorageSync('token') _this.goLoginPageTimeOut() } }) } /** * Creative contest article details page - initialize parameters first */ onLoad: function (options) { var that = this; var str = options.detail; var detail = str.split("|"); this.setData({ aid1: detail[0], time: detail[2], topImg: detail[3], }) const _this = this const token = wx.getStorageSync('token'); if (!token) { _this.goLoginPageTimeOut() return } /** * token Inspection **/ WXAPI.checkToken(token).then(function (res) { console.log(res) if (res.code != 1000) { wx.removeStorageSync('token') _this.goLoginPageTimeOut() } }) }
2. The onshow method is called every time the page is echoed. It jumps from the authorization page to the first page. The wx.navigateBack() method is called from the authorization page. The onshow method is called when the authorization succeeds and returns to the first page. This method is used to render data. The calling code is as follows
/** * The function in this method does not need to be called every time; to judge whether the call result exists, do not call */ onShow: function() { console.log('Investigation and research on show'); var that = this; that.judgeSQ(); that.imgFun(); } //Judgement authorization judgeSQ: function () { var that = this; that.setData({ sessionId: wx.getStorageSync('token') }) //If there are query results in the page, no call is made to prevent multiple calls if (that.data.ranks.length==0){ that.dataList(); } that.prizeList(); }
3. The problem of refreshing and leaving blank in pull-up and pull-down (both pull-up and pull-down are authorized to judge)
/** * Page related event processing function -- listening to user pull-down action */ onPullDownRefresh: function() { //Pull down and back automatically. The above will not be left blank wx.stopPullDownRefresh(); const _this = this const token = wx.getStorageSync('token'); console.log(token) if (!token) { _this.goLoginPageTimeOut() return } /** * token Inspection **/ WXAPI.checkToken(token).then(function (res) { if (res.code != 1000) { wx.removeStorageSync('token') _this.goLoginPageTimeOut() } }) var that = this ; wx.showLoading({ title: 'Loading', }) this.setData({ page1 : 1, ourselfs: [], ranks: [], noMoreHidden: true, }) setTimeout(function() { that.onShow(); wx.hideLoading(); }, 2000); }, /** * Handling function of page pull bottom event */ onReachBottom: function() { const _this = this const token = wx.getStorageSync('token'); if (!token) { _this.goLoginPageTimeOut() return } /** * token Inspection **/ WXAPI.checkToken(token).then(function (res) { console.log(res) if (res.code != 1000) { wx.removeStorageSync('token') _this.goLoginPageTimeOut() } }) wx.showLoading({ title: 'Loading', }) setTimeout(function () { _this.dataList1() wx.hideLoading(); }, 2000); /** var isLoading = this.data.isLoading; var rankslen = this.data.ranks.length; var page1 = this.data.page1; var that=this; if (isLoading == true && rankslen >= 10) { page1 = page1 + 1; that.setData({ page1: page1 }) } */ }
4. push the newly queried data into the previous result set
var ranks1 = that.data.ranks; for (var i = 0; i < res.data.ranks.length; i++) { ranks1.push(res.data.ranks[i]); }
5. I have used some skills to like the list data and change the style. The code is as follows:
//After you click like if (res.code == 4000) { //The index - 1 used to receive likes does not have a corresponding index by default and cannot be 0 because the index start value is 0 var index = -1; for (var i = 0; i < ourselfs.length; i++) { if (ourselfs[i].art.aid == aid) { //Find the corresponding index of this article index = i; } } if (index != -1) { //Index find start update that.setData({ [`ourselfs[${index}].art.isthumbs`]: true, [`ourselfs[${index}].art.thumbs`]: num + 1 }) } wx.showToast({ title: "Praise for success", icon: 'none', duration: 2000 }) } else { wx.showToast({ title: res.msg, icon: 'none', duration: 2000 }) }