开始
把这块封装成一个组件
封装之前,先把数写成数组的形式。
评论和分享 数字就可以
用户点一次就变成黄色。顶和踩 只能是一次。
顶和踩的数量
播放次数和时长
list:[
{
userpic:"../../static/demo/userpic/12.jpg",
username:"昵称",
isguanzhu:false,
title:"我是标题",
type:"img", // img:图文,video:视频
titlepic:"../../static/demo/datapic/11.jpg",
infonum:{
index:0,//0:没有操作,1:顶,2:踩;
dingnum:11,
cainum:11,
},
commentnum:10,
sharenum:10,
},
{
userpic:"../../static/demo/userpic/12.jpg",
username:"昵称",
isguanzhu:true,
title:"我是标题",
type:"video", // img:图文,video:视频
titlepic:"../../static/demo/datapic/11.jpg",
playnum:"20w",
long:"2:47",
infonum:{
index:1,//0:没有操作,1:顶,2:踩;
dingnum:11,
cainum:11,
},
commentnum:10,
sharenum:10,
}
]
剪切放到block块里面。注意最外层还是要套一层view
<block v-for="(item,index) in list" :key="index">
替换对应的字段
是否关注
标题和主图
视频用template包裹起来。 判断类型是否是视频。如果是才显示视频。
顶和踩
评论数和转发数
增加一个选中的样式
.index-list4 .active,.index-list4 .active>view{
color: #c5f61c;
}
关注改用v-show
<view class="u-f-ac" v-show="item.isguanzhu">
<view class="icon iconfont icon-zengjia"></view>关注
</view>
:class="{'active':(item.infonum.index==1)}"
:class="{'active':(item.infonum.index==2)}"
开始封装组件
新建组件
把它移动到index目录下
把我们之前的代码 复制过来。注意复制的是class=index-list开始的。
css也复制进来。
然后定义props属性
<script>
export default {
props: {
item: Object
}
}
</script>
引入组件
import indexList from '@/components/index/index-list.vue';
注册组件
components: {
indexList
},
使用组件
加一个索引的属性
<index-list :item="item" :index="index"></index-list>
封装的组件完整代码 index-list
css限制作用域
打包后css可能会有重复。
直接scoped表示这个组件内的css只能给当前这个组件使用。
最终代码
组件代码
<template>
<view class="index-list">
<view class="index-list1 u-f-ac u-f-jsb">
<view class="u-f-ac">
<image :src="item.userpic" mode="widthFix" lazy-load></image>
{{item.username}}
</view>
<view class="u-f-ac" v-show="item.isguanzhu">
<view class="icon iconfont icon-zengjia"></view>关注
</view>
</view>
<view class="index-list2">{{item.title}}</view>
<view class="index-list3 u-f-ajc">
<!-- 图片 -->
<image :src="item.titlepic" mode="widthFix" lazy-load>
</image>
<!-- 视频 -->
<template v-if="item.type=='video'">
<view class="index-list-play icon iconfont icon-bofang"></view>
<view class="index-list-playinfo">
{{item.playnum}}播放 {{item.long}}
</view>
</template>
</view>
<view class="index-list4 u-f-ac u-f-jsb">
<view class="u-f-ac">
<view class="u-f-ac" :class="{'active':(item.infonum.index==1)}">
<view class="icon iconfont icon-icon_xiaolian-mian"></view>
{{item.infonum.dingnum}}
</view>
<view class="u-f-ac" :class="{'active':(item.infonum.index==2)}">
<view class="icon iconfont icon-kulian"></view>
{{item.infonum.cainum}}
</view>
</view>
<view class="u-f-ac">
<view class="u-f-ac">
<view class="icon iconfont icon-pinglun1"></view>
{{item.commentnum}}
</view>
<view class="u-f-ac">
<view class="icon iconfont icon-zhuanfa"></view>
{{item.sharenum}}
</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
item: Object,
index: Number
}
}
</script>
<style scoped>
.index-list {
padding: 20upx;
border-bottom: 1upx solid #EEEEEE;
}
.index-list1 {
/* display: flex; */
/* justify-content: space-between; */
/* align-items: center; */
}
.index-list1>view:first-child {
/* display: flex;
align-items: center; */
color: #999999;
}
.index-list1>view:first-child image {
width: 95upx;
height: 90upx;
border-radius: 100%;
margin-right: 10upx;
}
.index-list1>view:last-child {
/* display: flex;
align-items: center; */
background: #F4F4F4;
border-radius: 5upx;
padding: 0 10upx;
}
.index-list2 {
padding-top: 15upx;
font-size: 32upx;
}
.index-list3 {
position: relative;
padding-top: 15upx;
}
.index-list3>image {
width: 100%;
border-radius: 20upx;
}
.index-list-play {
position: absolute;
font-size: 140upx;
color: #FFFFFF;
}
.index-list-playinfo {
position: absolute;
background: rgba(51, 51, 51, 0.72);
color: #FFFFFF;
bottom: 8upx;
right: 8upx;
border-radius: 40upx;
font-size: 22upx;
padding: 0 12upx;
}
.index-list4 {
padding: 15upx 0;
/* display: flex; */
/* justify-content: space-between; */
/* align-items: center; */
}
.index-list4 view {
color: #999999;
}
.index-list4>view:first-child {
/* display: flex;
align-items: center; */
}
.index-list4>view:last-child {
/* display: flex;
align-items: center; */
}
.index-list4>view>view {
/* display: flex;
align-items: center; */
}
.index-list4>view>view>view,
.index-list4>view>view:first-child {
margin-right: 10upx;
}
.index-list4 .active,.index-list4 .active>view{
color: #c5f61c;
}
</style>
首页代码
<template>
<view>
<block v-for="(item,index) in list" :key="index">
<index-list :item="item" :index="index"></index-list>
</block>
</view>
</template>
<script>
import indexList from '@/components/index/index-list.vue';
export default {
components: {
indexList
},
data() {
return {
list: [{
userpic: "../../static/demo/userpic/12.jpg",
username: "昵称",
isguanzhu: false,
title: "我是标题",
type: "img", // img:图文,video:视频
titlepic: "../../static/demo/datapic/11.jpg",
infonum: {
index: 0, //0:没有操作,1:顶,2:踩;
dingnum: 11,
cainum: 11,
},
commentnum: 10,
sharenum: 10,
},
{
userpic: "../../static/demo/userpic/12.jpg",
username: "昵称",
isguanzhu: true,
title: "我是标题",
type: "video", // img:图文,video:视频
titlepic: "../../static/demo/datapic/11.jpg",
playnum: "20w",
long: "2:47",
infonum: {
index: 1, //0:没有操作,1:顶,2:踩;
dingnum: 11,
cainum: 11,
},
commentnum: 10,
sharenum: 10,
}
]
}
},
onLoad() {
},
methods: {
}
}
</script>
<style>
</style>