小程序学习之旅---跳转传值,请求接口,图片显示,页面传值到详情页来获取内容

<view class='content'>

   <view class='warp' wx:for="{{list}}" wx:for-item='item' wx:key='{{index}}'>
        <view class='h2'>{{item.title}}</view>

        <view class='list'>
          <view class='item' wx:for="{{item.list}}" wx:for-item="food" wx:key='{{food._id}}'> 
            <!--<image src='{{host}}{{food.img_url}}'></image>-->
              <text>{{food.title}}</text>
          </view>

        </view>
    
    </view>

</view>
// pages/food/food.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    host: 'http://a.itying.com/'
  },
  goFoodContent (e) {
    console.log(e.target.dataset.aid)
    var aid = e.target.dataset.aid
    wx.navigateTo({
      url: '../foodcontent/foodcontent?aid=' + aid,
      success: (result) => {

      },
      fail: () => { },
      complete: () => { }
    });
  },
  requestData () {
    var that = this;
    wx.request({
      url: 'http://a.itying.com/api/productlist', //仅为示例,并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        //用到this要注意this指向
        that.setData({
          list: res.data.result
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取上个页面传过来的值
    console.log(options)
    this.requestData();
  }
})

传值到详情页

// pages/foodcontent/foodcontent.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    host: 'http://a.itying.com/'
  },
  requestData (id) {
    var id = id
    console.log(id)
    var that = this;
    var api = 'http://a.itying.com/api/productcontent?id=' + id;
    wx.request({
      url: api, //仅为示例,并非真实的接口地址     
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var data = res.data.result[0];
        data.img_url = data.img_url.replace(/\\/g, '/');
        that.setData({
          list: data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取上一个页面的传值
    console.log(options)
    var id = options.id
    this.requestData(id)
  }
})

详情页展示

<view class='p_img'>
  <image src='{{host}}{{list.img_url}}'></image>
</view>
<view class='h2'>
  {{list.title}}
</view>
<view class='content'>
  {{list.content}}
</view>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞朋哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值