小程序页面之间的通信

 

 

 

 

 

一:onShow/onHide + localStorage

// pageA
let isInitSelfShow = true;

Page({
  data: {
    helloMsg: 'hello from PageA'
  },

  onShow() {
    // 页面初始化也会触发onShow,这种情况可能不需要检查通信
    if (isInitSelfShow) return;

    let newHello = wx.getStorageSync('__data');

    if (newHello) {
      this.setData({
        helloMsg: newHello
      });

      // 清队上次通信数据
      wx.clearStorageSync('__data');
    }

  },

  onHide() {
    isInitSelfShow = false;
  },

  goC() {
    wx.navigateTo({
      url: '/pages/c/c'
    });
  }
});
// pageC
Page({
  doSomething() {
    wx.setStorageSync('__data', 'hello from PageC');
  }
});

二:

onShow/onHide + 小程序globalData

// PageA
let isInitSelfShow = true;
let app = getApp();

Page({
  data: {
    helloMsg: 'hello from PageA'
  },

  onShow() {
    if (isInitSelfShow) return;

    let newHello = app.$$data.helloMsg;

    if (newHello) {
      this.setData({
        helloMsg: newHello
      });

      // 清队上次通信数据
      app.$$data.helloMsg = null;
    }

  },

  onHide() {
    isInitSelfShow = false;
  },

  goC() {
    wx.navigateTo({
      url: '/pages/c/c'
    });
  }
});
// PageC
let app = getApp();

Page({
  doSomething() {
    app.$$data.helloMsg = 'hello from pageC';
  }
});

 三使用URl:

handleClickFileDetail(e) {
    wx.navigateTo({
      url: '/pages/fileDetail/fileDetail?detailID=' + e.currentTarget.dataset.id + '&status=' + e.currentTarget.dataset.status + '&index=true'
    })
  },
onLoad: function (options) 
options是包含数据的对象

 

posted on 2022-08-08 19:34  马玉豪  阅读(48)  评论(0)    收藏  举报

导航