【小程序】004 父子组件数据双向绑定

举报
LongYorke 发表于 2021/01/30 00:12:01 2021/01/30
【摘要】 父子组件数据双向绑定 音乐小程序歌单组件的实现

以一个tabBar为例解释父子组件传值

实现自定义组件思维导图

理解父子组件传值有一定难度,小伙伴们可以尝试在白纸上默写父子传值的路径与方法

如果图片被压缩模糊了可以点击  父子组件思维导图   查看

1、创建一个页面:

2、创建组件:

3、myComponent.json声明组件:

4、mypage.json声明使用组件

5、myComponent中配置组件wxml

6、myComponent中配置组件wxss

此处使用flex布局
.active:实现选中变红样式

7、myComponent中配置组件js
组件.js中是component对象
页面.js中是page对象

8、myPage.wxml中使用组件

9、myPage.js中处理逻辑


回到音乐小程序

下面我们进行歌单组件的实现
1、在页面playlist.json声明使用自定义组件“L-playlist”,且使用组件所处的相对路径

2、在页面playlist.js的data中存放歌单列表
比如:
playlist:[
      {"_id":"08560c9e5d042a5c0174f1ca26f1d7b2","copywrier":"热门推荐","playCount":1.4641238e+06,"highQuality":false,"type":0.0,"canDislike":true,"name":"天气转热了,适合听点凉爽的歌。","alg":"cityLevel_unknow","createTime":{"$date":"2019-06-14T23:14:36.746Z"},"id":2.780381322e+09,"picUrl":"https://p2.music.126.net/Biky7TE4CtW6NjGuqoUKZg==/109951164041827987.jpg","trackCount":53.0},
      {"_id":"08560c9e5d042a5c0174f1da7aa357aa","highQuality":false,"copywriter":"热门推荐","canDislike":true,"playCount":622822.6,"id":2.740107647e+09,"name":"「时空潜行」囿于昼夜的空想主义者","type":0.0,"alg":"cityLevel_unknow","createTime":{"$date":"2019-06-14T23:14:36.955Z"},"picUrl":"https://p2.music.126.net/Q0eS0avwGK04LufWM7qJug==/109951164116217181.jpg","trackCount":20.0},
      {"_id":"08560c9e5d042a5c0174f1de21c7e79e","id":2.828842343e+09,"type":0.0,"name":"粤语情诗:与你听风声,观赏过夜星","picUrl":"https://p2.music.126.net/K9IcG8cU6v4_SwuQ_x2xMA==/109951164124604652.jpg","highQuality":false,"alg":"cityLevel_unknow","playCount":1.785097e+06,"trackCount":52.0,"copywriter":"热门推荐","canDislike":true,"createTime":{"$date":"2019-06-14T23:14:36.982Z"}},
      {"_id":"08560c9e5d042a5d0174f1e67d1bb16f","playCount":7.719329e+06,"highQuality":false,"trackCount":950.0,"alg":"cityLevel_unknow","id":9.17794768e+08,"type":0.0,"name":"翻唱简史:日本四百首","canDislike":true,"createTime":{"$date":"2019-06-14T23:14:37.037Z"},"copywriter":"热门推荐","picUrl":"https://p2.music.126.net/NczCuurE5eVvObUjssoGjQ==/109951163788653124.jpg"},
      {"_id":"08560c9e5d042a5d0174f1ea32c4c288","type":0.0,"copywriter":"热门推荐","highQuality":false,"createTime":{"$date":"2019-06-14T23:14:37.097Z"},"id":2.201879658e+09,"alg":"cityLevel_unknow","playCount":1.06749088e+08,"name":"你的青春里有没有属于你的一首歌?","picUrl":"https://p2.music.126.net/wpahk9cQCDtdzJPE52EzJQ==/109951163271025942.jpg","canDislike":true,"trackCount":169.0},
      {"_id":"08560c9e5d0829820362a79f4b049d2d","alg":"cityLevel_unknow","name":"「乐队的夏天」参赛歌曲合集丨EP04更新","highQuality":false,"picUrl":"http://p2.music.126.net/2WE5C2EypEwLJd2qXFd4cw==/109951164086686815.jpg","trackCount":158.0,"createTime":{"$date":"2019-06-18T00:00:02.553Z"},"copywriter":"热门推荐","playCount":1.5742008e+06,"canDislike":true,"id":2.79477263e+09,"type":0.0}
    ]

3、在页面playlist.wxml中使用block对L-playlist进行wx遍历



4、在组件playlist.wxml中填好固定样式,并通过绑定组件playlist.js中properties的内容
<!-- 歌单列表 -->
<view class="play-container">
  <block wx:for="{{playlist}}" wx:key="_id"> 
    <L-playlist playlist="{{item}}"></L-playlist>
  </block>
</view>

5、组件playlist.js中properties的内容用于存放从页面playlist.js通过属性=“值”传递过来的内容,注意要声明存放内容类型type:Object,其中value:{}是无任何操作下的默认值
  properties: {
    playlist:{
      type:Object,
      value:{}
    }
  },

6、用定位做右上角的听歌数目,注意耳机图片其实是使用背景图background,小程序中背景图目前不允许使用网络图片,需要使用小程序本地的图片,我们可以将图片转变为base64格式,再放入url中。有些background会重复,使用样式去重
background-repeat: no-repeat; 

图片转变为base64格式网站:https://c.runoob.com/front-end/59


7、wxss样式
组件样式负责一个图片框架样式
.playlist-container{
  width: 220rpx;
  position: relative;
  padding-bottom: 20rpx;
}

.playlist-image{
  width: 100%;
  height: 220rpx;
  border-radius: 6rpx;
}

.playlist-playcount{
  font-size: 24rpx;
  color:#fff;
  text-shadow: 1px 0 0 rgba(0, 0,0,0.15)
  position: absolute;
  right: 10rpx;
  top: 4rpx;
  padding-left: 26rpx;
  background: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyMiAyMCI+PGcgb3BhY2l0eT0iLjE1Ij48cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGZpbGw9IiMwNDAwMDAiIGQ9Im0yMiAxNi43NzdjMCAxLjIzMy0xLjEyMSAyLjIzMy0yLjUwNiAyLjIzMy0xLjM4NCAwLTIuNTA2LTEtMi41MDYtMi4yMzN2LTIuNTUzYzAtMS4yMzQgMS4xMjItMi4yMzMgMi41MDYtMi4yMzMuMTc0IDAgLjM0My4wMTcuNTA2LjA0NnYtMS4zN2gtLjAzM2MuMDE3LS4yMi4wMzMtLjQ0MS4wMzMtLjY2NiAwLTQuNDE4LTMuNTgyLTgtOC04LTQuNDE4IDAtOCAzLjU4Mi04IDggMCAuMjI1LjAxNi40NDYuMDM0LjY2NmgtLjAzNHYxLjM3Yy4xNjMtLjAyOS4zMzMtLjA0Ni41MDUtLjA0NiAxLjM4NCAwIDIuNTA2Ljk5OSAyLjUwNiAyLjIzM3YyLjU1M2MwIDEuMjMzLTEuMTIyIDIuMjMzLTIuNTA2IDIuMjMzcy0yLjUwNS0uOTk5LTIuNTA1LTIuMjMzdi0yLjU1M2MwLS4yNTguMDU5LS41MDEuMTQ4LS43My0uMDg1LS4xND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A0LjQ3Ny0xMCAxMC0xMCA1LjUyMyAwIDEwIDQuNDc3IDEwIDEwIDAgLjA5LS4wMDkuMTc4LS4wMTQuMjY2LjAwMi4wMjQuMDE0LjA0NC4wMTQuMDY3djJjMCAuMzA2LS4xNDUuNTY5LS4zNi43NTMuMjI0LjMzNC4zNi43Mi4zNiAxLjEzOHYyLjU1MiIvPjwvc3ZnPg==);
}


.playlist-name{
  font-size: 26rpx;
  line-height: 1.2;
  padding: 2px 0 0 6px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
}
如果歌单图片下面的文字过长,可以使用-webkit-box将过长的内容隐藏并以...代替
-webkit-box-orient: vertical决定从顶部向底部垂直布置子元素
-webkit-line-clamp决定超出第几行开始隐藏文字



组件样式负责多个图片框架整体排版
.playlist-container{
 display: flex;
 flex-wrap: wrap;
 justify-content: space-around;
 margin-top: 10rpx;
 flex-direction: row;
}

注意:tabBar组件例子参考自黑马教程,此处用来学习
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。