11.20 小程序开发总结 (1)

小程序接口:

1.getCurrentPages  获取页面前几个页面的所有app.js 信息 (太强了!!我竟然才知道)

    用途:获取上个页面的函数来获取做数据交互, 类似react 父级给子级传参,函数等.

 代码:

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    

此时获取到上个页面所有的函数,及其参数;

2.小程序二维码传参,分享传参

 需求:新版小程序中分享(转发)接口调整,无法携带参数字段,若要在从转发链接中获取所需的参数无法实现;

 解决:在跳转该页面时带上所需的参数,这样转发时即会带上这些参数,只要在获取分享页面时在onload中获取数据即可解决参数无法传递问题;

 例: 一个商品的详情 需要我分享出去后在另一个用户打开时获取到是从谁分享的;

  我们就可以在商品列表页点击商品跳转时给其拼接上该用户的id,这样分享详情时即会带上我们所有的参数;

 代码:

  onLoad: function (options) {
    let Id, userId, scene;
    if (options.scene){
      scene = decodeURIComponent(options.scene);
    }
      if (!util.isNull(scene)) {
        let sceneStr = scene.split('_');
        Id = util.isNull(sceneStr[1]) ? null : sceneStr[1];
        userId = util.isNull(sceneStr[2]) ? null : sceneStr[2];
      } else {
        Id = util.isNull(options.houseId) ? null : options.Id;
        userId = util.isNull(options.userId) ? null : options.userId;
      }
    if (app.globalData.userId && !userId){
       userId = app.globalData.userId;
     }
    this.onGetHouseReferrer(Id, userId);
}

 详情中onload的代码

 

  

 

posted @ 2018-11-20 17:52  hhling  阅读(129)  评论(0编辑  收藏  举报