当前位置:首页 > VUE

vue公告通知页面实现

2026-02-24 02:30:55VUE

实现公告通知页面的基本思路

公告通知页面通常需要展示公告列表、公告详情,可能包含分页、未读标记等功能。Vue.js结合后端API可以实现动态数据交互。

页面结构设计

使用Vue单文件组件结构,通常包含以下部分:

vue公告通知页面实现

  • 公告列表区域
  • 分页控件
  • 公告详情弹窗
  • 状态标记(如未读红点)
<template>
  <div class="notice-container">
    <h3>公告列表</h3>
    <ul class="notice-list">
      <li v-for="item in notices" :key="item.id" @click="showDetail(item)">
        <span :class="{ 'unread': !item.isRead }">{{ item.title }}</span>
        <span class="date">{{ formatDate(item.createTime) }}</span>
      </li>
    </ul>
    <el-pagination
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="handlePageChange">
    </el-pagination>
    <el-dialog :title="currentNotice.title" :visible.sync="dialogVisible">
      <div v-html="currentNotice.content"></div>
    </el-dialog>
  </div>
</template>

数据获取与处理

通过axios调用后端API获取公告数据,处理分页和状态更新:

vue公告通知页面实现

<script>
import axios from 'axios';

export default {
  data() {
    return {
      notices: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      currentNotice: {},
      dialogVisible: false
    }
  },
  created() {
    this.fetchNotices();
  },
  methods: {
    async fetchNotices() {
      const res = await axios.get('/api/notices', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      });
      this.notices = res.data.list;
      this.total = res.data.total;
    },
    showDetail(item) {
      this.currentNotice = item;
      this.dialogVisible = true;
      if (!item.isRead) {
        this.markAsRead(item.id);
      }
    },
    async markAsRead(id) {
      await axios.patch(`/api/notices/${id}/read`);
      const index = this.notices.findIndex(item => item.id === id);
      this.$set(this.notices[index], 'isRead', true);
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.fetchNotices();
    },
    formatDate(date) {
      return new Date(date).toLocaleDateString();
    }
  }
}
</script>

样式设计

使用CSS或SCSS美化公告列表:

<style lang="scss" scoped>
.notice-container {
  padding: 20px;

  .notice-list {
    list-style: none;
    padding: 0;

    li {
      padding: 12px 16px;
      border-bottom: 1px solid #eee;
      cursor: pointer;
      display: flex;
      justify-content: space-between;

      &:hover {
        background-color: #f5f5f5;
      }

      .unread {
        color: #f56c6c;
        font-weight: bold;
      }

      .date {
        color: #999;
        font-size: 12px;
      }
    }
  }
}
</style>

功能扩展建议

  1. 分类筛选:添加公告类型筛选功能
  2. 搜索功能:实现标题关键字搜索
  3. 置顶公告:区分普通公告和置顶公告
  4. 批量操作:添加批量标记已读功能
  5. 实时推送:集成WebSocket实现新公告实时提醒

后端接口建议

典型RESTful接口设计:

  • GET /api/notices - 获取公告列表
  • GET /api/notices/:id - 获取公告详情
  • PATCH /api/notices/:id/read - 标记为已读
  • POST /api/notices - 创建新公告(管理员用)

可根据实际需求调整接口参数和返回数据结构。

标签: 页面通知
分享给朋友:

相关文章

vue 实现页面跳转

vue 实现页面跳转

vue 实现页面跳转的方法 在 Vue 中实现页面跳转主要通过路由(Vue Router)完成,以下是几种常见的方式: 声明式导航(模板中使用 <router-link>) 在模板中直接…

vue实现关闭页面

vue实现关闭页面

关闭当前页面的方法 在Vue中关闭当前页面通常涉及调用浏览器提供的JavaScript方法。以下是几种常见的实现方式: window.close()方法 methods: { closeWin…

实现js页面跳转

实现js页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现跳转,这是最常用的方法: window.location.href = "https://…

vue页面布局实现

vue页面布局实现

Vue 页面布局实现方法 基础布局结构 使用 Vue 单文件组件(SFC)构建页面框架,结合 <template>、<script> 和 <style> 三个部分。…

css制作页面

css制作页面

使用CSS制作页面的基本方法 CSS(层叠样式表)用于控制网页的样式和布局。通过CSS可以实现页面元素的定位、颜色、字体、间距等视觉效果。 HTML结构基础 在开始CSS之前,需要先构建HTML结构…

js怎么实现网页跳转页面跳转页面跳转

js怎么实现网页跳转页面跳转页面跳转

JavaScript 实现网页跳转的方法 使用 window.location.href 跳转 通过修改 window.location.href 属性实现页面跳转,这是最常用的方式。例如: w…