微信小程序传递对象(数组)

      从接口获得的数组需要传递给下一个页面,需要通过var items = JSON.stringify(res.data)这句代码将对象转为字符串后进行传递。


  search: function (res) {
    console.log("搜索信息---" + this.data.searchValue)
    var self = this
    wx.request({
      url: 'http://127.0.0.1:8080/fybpi/pi.Goods.search.hf',
      data: {
        MemberID: 1              ,                //用户编号 
        goodName: this.data.searchValue            //搜索内容     
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        var items = JSON.stringify(res.data);
        self.setData({
          items: res.data
        })
        if (res.data.length != 0) {
          var url = '../home/search/search?items=' + items;
        }
        else {
          var url = '../home/searchno/searchno';
        }
        wx.navigateTo({
          url: url,
        })
      }
    })
  },

传递到第二个页面。通过这句代码 var items = JSON.parse(this.options.items);将字符串转换为对象进行操作。(onload接收上个页面的内容代码是options.items,onshow需要加一个this)

onShow: function (options) {
    var self = this
    var items = JSON.parse(this.options.items);
    console.log("search show", items)
    self.setData({
      items: items
    })
  },

猜你喜欢

转载自blog.csdn.net/qq_39404258/article/details/90479569