快速编写一个消息列表组件 uniapp篇

快速编写一个消息列表组件 uniapp篇

  • 首先创建一个消息列表页面 msg,vue
    • 编写好页面样式
<template>
	<view>
		<template v-if="list.length >0 ">
		<block v-for="(item,index) in list" :key="index">
			<msg-list :item="item" :index="index"></msg-list>
		</block>
		</template>
		<template v-else>
			<no-thing></no-thing>
		</template>
	</view>
</template>

<script>
	import noThing from '../../components/common/no-thing.vue';
	import msgList from '@/components/msg/msg-list.vue';
	const demo=[{
					avatar:"../../static/imgs/ali_pay.png",
					username:"昵称",
					update_time:"1594187992",
					data:"内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容",
					noread:20
				},
				{
					avatar:"../../static/imgs/ali_pay.png",
					username:"昵称",
					update_time:"1594187992",
					data:"内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容",
					noread:20
				},
				{
					avatar:"../../static/imgs/ali_pay.png",
					username:"昵称",
					update_time:"1594187992",
					data:"内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容",
					noread:20
				}];
	export default {
		components:{
			msgList,
			noThing
		},
		data() {
			return {
				list:[]
			}
		},
		//监听下拉刷新
		onPullDownRefresh() {
			this.refresh()
		},
		methods: {
			//下拉刷新
			refresh(){
				setTimeout(()=>{
					this.list = demo
					//停止下拉刷新
					uni.stopPullDownRefresh();
				},2000)
			}
		}
	}
</script>

<style>

</style>

新建组件 msg-list.vue
 <template>
 <!-- 消息列表 -->
 
 <view class="flex align-center p-2 border-bottom border-light-secondary" hover-class="bg-light">
 	<image :src="item.avatar" class="rounded-circle mr-2" style="height: 80rpx;width: 80rpx;" mode=""></image>
 	<view class="flex flex-column flex-1 ">
 		<view class="flex align-center justify-between">
 			<text class="font-md">{{item.username}}</text>
 			<text class="font-sm text-secondary">{{item.update_time|formatTime}}</text>
 		</view>
 		<view class="flex align-center justify-between">
 			<text class="text-secondary text-ellipsis" style="max-width: 500rpx;">{{item.data}}</text>
 			<uni-badge :text="item.noread" type="error"></uni-badge>
 		</view>
 		
 	</view>
 </view>
</template>
<script>
 import uniBadge from '../uni-ui/uni-badge/uni-badge.vue';
 import $T from '../../common/time.js';
 export default{
 	components:{
 		uniBadge
 		
 	},
 	props:{
 		item:Object,
 		index:Number
 	},
 	//过滤器
 	filters:{
 		formatTime(value){
 			return $T.gettime(value);
 		}
 	},
 }
</script>

<style>
</style>

运行项目效果图如下
在这里插入图片描述

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面我来给你介绍一下如何封装一个listcell组件。 1. 创建组件文件 首先,在uniapp的项目中,选择需要封装listcell组件的页面。在该页面所在的目录下,创建一个名为list-cell的组件文件夹。 2. 编写组件结构 在list-cell文件夹下,创建一个名为list-cell.vue的Vue单文件组件。在该组件中,可以编写自定义的listcell组件结构。 例如: ```html <template> <view class="list-cell"> <view class="list-cell__left">{{ left }}</view> <view class="list-cell__right">{{ right }}</view> </view> </template> <script> export default { name: 'ListCell', props: { left: String, right: String } } </script> <style> .list-cell { display: flex; justify-content: space-between; align-items: center; padding: 20rpx; background-color: #fff; border-bottom: 1px solid #ddd; } .list-cell__left { font-size: 30rpx; color: #333; } .list-cell__right { font-size: 28rpx; color: #666; } </style> ``` 3. 在页面中引入组件 在需要使用listcell组件的页面中,引入该组件。例如,在index.vue页面中,可以引入listcell组件。 ```html <template> <view> <list-cell left="姓名" right="张三"></list-cell> <list-cell left="年龄" right="18"></list-cell> <list-cell left="性别" right="男"></list-cell> </view> </template> <script> import ListCell from '@/components/list-cell/list-cell.vue' export default { components: { ListCell }, } </script> ``` 这样,当在index.vue页面中使用list-cell组件时,就可以自动引入并使用该组件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值