(干货)小程序项目——泡泡云音乐day3

5 篇文章 0 订阅
2 篇文章 0 订阅

十一、歌手详情页的跳转传参

分析:这里使用通信通道wx.navitageTo()独有的进行传参,通过eventChannel向子页面“singerDetail”页面传送数据,

1、先在“热门歌手”里绑定bindtap事件,传递数据索引(见六),定义一个自定义事件hotlink在逻辑层通过一个函数来实现跳转,在block中设置,里面的view中引用就可以了。

<!-- 热门歌手 -->
<view class="title">热门歌手</view>
<view class="hotSinger">
  <block wx:for="{{singer}}" wx:key="this" wx:for-index="gs">
  <!-- 绑定自定义事件hotlink -->
    <view class="item" bindtap="hotlink" data-index="{{gs}}">
      <image src="{{item.picUrl}}"></image>
      <text>{{item.name}}</text>
    </view>
  </block>
</view>

2、index.js拿到index.wxml中的自定义事件hotLink,像子页面(singerDetail.js)传参

  // 点击跳转歌手详情页
  hotlink:function (p) {
    // 找到歌手的下标索引
    const index = p.currentTarget.dataset.index;
    // 拿到当前数据
    const singer = this.data.singer
    //传参
    wx.navigateTo({
      url: '../singerDetail/singerDetail',
      // events:events,
      success:(res)=>{
        // 通过eventChannel把歌手的索引传给子页面
        res.eventChannel.emit('acceptDataFromOpenerPage',{data:singer[index]})
      },
    })
  },

在这里插入图片描述

点进去到子页面,控制台输出这些字样,就拿到首页传过来的东西了

3、在页面singerDetail.js中找到监听页面加载函数,通过acceptDataFromOpenerPage接收首页传来的数据

数据层定义singerData接收当前歌手的数据

  data: {
    // 当前歌手的数据
     singerData:{}
  },
// 这里接收主页传来的数据,并进行存储
  onLoad: function(options) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', data=>{
      // 这里打印的就是父页面传来的数据
      console.log(data)
      this.setData({
        // 把获取的data赋给上面定义的singerData{}
        singerData: data
      })
    })
    // 调用渲染页面的方法
     this.getSingerDetail()
  },

4、数据获取

getSingerDetail:function(){
    //  这里打印的是上面数据层的data,即window下data的数据
     //console.log(this.data);
    var id = this.data.singerData.data.id
     //console.log(id);
   },

此时,歌手id就已经拿到了

5、以id为变量进行不同歌手的数据信息请求,并储存到geshouDetail:{}当中

   wx.request({
     url:'http://localhost:3000/artist/detail?id='+id,
     dataType:'json',
     method:'GET',
     success:(res)=>{
      //  console.log(res.data.data)
      this.setData({
        // 储存数据
        geshouDetail:res.data.data
      })
     }
   })
  },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值