小程序开发Tips

这篇文章主要聊聊小程序开发。

小程序开发有几个点在文档里面没有写的很明确,在这里我更明确的提一下。

带着参数跳转

小程序之间的页面跳转携带参数的小例子:

<navigator url="" bindtap="bindViewTap">

这个navigator是一个跳转,绑定了bindViewTap方法。bindViewTap方法实现如下:

  var bindViewTap = function () {
    console.log('run search.');
    let location = {
      x: 1,
      y: 1
    };
    let keyword = 'test';
    wx.navigateTo({
      url: `../map/map?latitude=${location.x}&longitude=${location.y}&keyword=${keyword}`
    })
  }

如此一来,便携带着三个参数,就像是网页GET请求一样,跳转到../map/map页面。此时,map页面需要接收这几个参数。示例代码:

  var onLoad = function (options) {
    let that = this;
    console.log('Map load.');
    this.setData({
      markers: {
        latitude: options.latitude,
        longitude: options.longitude,
      },
      inputVal: options.keyword,
      init: true
    }, function() {
      // 完成函数后回调
      that.showInput();
    });
  }

在Page的OnLoad方法中,可以从options参数里面把传递的值拿出来,在map页面中使用。

本文后续持久更新。


也可以看看