通知列表页是移动应用中常见的功能,用于展示用户收到的通知信息,如系统消息、点赞提醒、关注通知等。它为用户提供了一个统一的入口,方便用户查看和管理通知。
本代码实现了一个简单的通知列表页,包含以下基本功能:
首先,我们搭建了通知列表页的组件结构:
<template>
<div class="notification-page">
<div class="notification-header">
...
</div>
<div class="notification-content">
<div
class="notification-item"
v-for="item in notificationList"
:key="item.id"
>
...
</div>
</div>
</div>
</template>
然后,我们使用 ref
绑定了通知列表数据:
const notificationList = ref([
...
]);
接下来,我们在 v-for
循环中渲染了通知列表项:
<div
class="notification-item"
v-for="item in notificationList"
:key="item.id"
>
...
</div>
以下是关键代码的分析:
// 点击通知进入详情页
const handleClickNotification = (item) => {
router.push({
path: `/notification/${item.id}`,
state: { item },
});
};
此代码实现了点击通知进入详情页的功能。
// 下拉刷新
const onRefresh = () => {
// 模拟异步请求
setTimeout(() => {
notificationList.value = [
...notificationList.value,
...[
{
id: 5,
avatar: "https://source.unsplash.com/random/50x50",
title: "Jennie Dean liked 4 of your photos",
time: "10 min ago",
},
{
id: 6,
avatar: "https://source.unsplash.com/random/50x50",
title: "Garrett Gilbert started following you",
time: "1 hour ago",
},
],
];
vm.$nextTick(() => {
vm.$refs.notificationContent.finishRefresh();
});
}, 1000);
};
此代码实现了下拉刷新的功能。
// 上拉加载更多
const onInfiniteScroll = () => {
// 模拟异步请求
setTimeout(() => {
notificationList.value = [
...notificationList.value,
...[
{
id: 7,
avatar: "https://source.unsplash.com/random/50x50",
title: "Jennie Dean left you a comment",
time: "1 hour ago",
},
{
id: 8,
avatar: "https://source.unsplash.com/random/50x50",
title: "Thomas Zack Edison liked 4 of your photos",
time: "1 day ago",
},
],
];
vm.$nextTick(() => {
vm.$refs.notificationContent.finishInfiniteScroll();
});
}, 1000);
};
此代码实现了上拉加载更多通知的功能。