1. 程式人生 > >小程式——帶參返回上一頁幾種方法

小程式——帶參返回上一頁幾種方法

小程式的頁面跳轉API像wx.navigateTo()、wx.redirectTo()之類的,都是跳轉到一個全新的頁面,當這個頁面是表單的時候,有時候需要跳轉到其它頁面選取資訊後再跳轉回來,那之前填的資訊就必須得存在。

小程式的頁面跳轉API像wx.navigateTo()、wx.redirectTo()之類的,都是跳轉到一個全新的頁面,當這個頁面是表單的時候,有時候需要跳轉到其它頁面選取資訊後再跳轉回來,那之前填的資訊就必須得存在,這個時候可以當跳轉到選擇資訊的頁面,選中資訊後設置設定上一頁的data,然後再返回到上一頁,這樣資料都會存在。

方法一 :

把當前頁面資料放入本地快取( wx.setStorage(wx.setStorageSync),上一個頁面再從快取中取出(wx.getStorage(wx.getStorageSync))同時退出登入時要清除快取(wx.clearStorage(wx.clearStorageSync))。處理不好的話容易出問題,不夠優雅,建議使用方法二。

方法二:

在當前頁設定上一頁的data,例如

var pages = getCurrentPages();
      // var currPage = pages[pages.length - 1];   // 當前頁面
      var prevPage = pages[pages.length - 2];  // 上一個頁面
     // 直接呼叫上一個頁面的setData()方法,把資料存到上一個頁面中去
      prevPage.setData({
        liveId: that.liveId
      });

當然這個“liveId”必須是上一頁有的資料才行,即必須在data裡有定義

在上一頁的onshow裡取資料:

onShow() {
      let that = this;
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];   // 當前頁面
      that.liveId = currPage.data.liveId;
}

直接呼叫方法名來更新資料 

頁面A:

Page({
     data: {
        name: ''
     },
     ...
     ,
     //更新name
     changeData: function(name){
        this.setData({
            name: name
        })
     }
})

頁面B,假設有一個文字框用於輸入姓名,點選返回按鈕後更新頁面A的name

Page({
    //此方法用於文字框輸入回撥
    inputTyping: function (e) {
        //獲取頁面棧
        var pages = getCurrentPages();
        if(pages.length > 1){
            //上一個頁面例項物件
            var prePage = pages[pages.length - 2];
            //關鍵在這裡
            prePage.changeData(e.detail.value)
        }
    }
})

這樣就可以實現資料傳遞給上一個頁面,要注意頁面A必須使用wx.navigateTo跳轉到頁面B,不能使用wx.redirectTo,這樣會關閉上一個頁面,導致頁面B無法獲取上一頁Page例項。

方法三: 
在app.js中設定全域性變數,當前頁賦值,上一頁取之。

例如:

globalData: {
    userInfo: null,
  }

注意:方法一,方法三,都需要重新重新整理頁面資料所走方法為:

/**
   * 生命週期函式--監聽頁面顯示
   */
  onShow: function () {
  },

另外補充兩種方法:

微信小程式通知廣播模式類,降低小程式開發的耦合度

使用例子

  • 主頁面註冊事件
  • // 引入WxNotificationCenter 工具
    const WxNotificationCenter = require("../../utils/WxNotificationCenter.js");
    
    // 回撥函式傳值
    onPickerCountry: function (country) {
        this.setData({
          country: country
        });
      },
      /**
       * 生命週期函式--監聽頁面載入
       */
      onLoad: function (options) {
        WxNotificationCenter.addNotification("testNotificationName", this.onPickerCountry, this);
      },
    
    // 跳轉到子頁面
      pickerCountry: function(){
        wx.navigateTo({
          url: 'b',
        })
      },
    
  • 子頁面觸發事件
  • // 同樣需要先引入WxNotificationCenter
    const WxNotificationCenter = require("../../utils/WxNotificationCenter.js");
    
    // 觸發事件,回傳資料
    bindPickerChange: function (e) {
        var index = e.detail.value;
        var country = this.data.countries[index];
        this.setData({ country: country})
        console.log('picker傳送選擇改變,攜帶值為', index)
        console.log('picker傳送選擇改變,攜帶值為', country.name)
    // 回傳資料
           WxNotificationCenter.postNotificationName("testNotificationName", country);
      },
    

    注意這裡面的 testNotificationName 是一個標識,類似click 是點選事件一樣,你也可以按照自己的業務或者操作來命名

是一個很簡單的事件分發的Javascript庫(僅僅 0.9kb),簡潔實用。

使用例子

  • 主頁面註冊事件
// 引入bonfire 工具
const onfire = require("../../utils/onfire.js");

// 回撥函式傳值
onPickerCountry: function (country) {
    this.setData({
      country: country
    });
  },
  /**
   * 生命週期函式--監聽頁面載入
   */
  onLoad: function (options) {
    var eventObj = onfire.on('eventName', this.onPickerCountry);
  },

// 跳轉到子頁面
  pickerCountry: function(){
    wx.navigateTo({
      url: 'b',
    })
  },
  • 子頁面觸發事件
// 同樣需要先引入onfire
const onfire = require("../../utils/onfire.js");

// 觸發事件,回傳資料
bindPickerChange: function (e) {
    var index = e.detail.value;
    var country = this.data.countries[index];
    this.setData({ country: country})
    console.log('picker傳送選擇改變,攜帶值為', index)
    console.log('picker傳送選擇改變,攜帶值為', country.name)
// 回傳資料
    onfire.fire('eventName', country);
  },

注意這裡面的 eventName 是一個標識,類似click 是點選事件一樣,你也可以按照自己的業務或者操作來命名

大家還有其他好的方法也可以在底下留言評論,未完待續!