当前位置:首页 > VUE

vue实现新闻列表

2026-02-19 19:54:48VUE

使用 Vue 实现新闻列表

数据准备

新闻列表通常需要从后端 API 获取数据,或使用本地模拟数据。以下是一个示例新闻数据格式:

const newsList = [
  {
    id: 1,
    title: "Vue 3.0 正式发布",
    content: "Vue 3.0 带来了更快的性能和更好的 TypeScript 支持",
    date: "2023-01-01",
    author: "Evan You"
  },
  {
    id: 2,
    title: "Composition API 详解",
    content: "新的 Composition API 提供了更灵活的代码组织方式",
    date: "2023-01-02",
    author: "Vue 团队"
  }
]

组件结构

创建一个新闻列表组件 NewsList.vue

vue实现新闻列表

<template>
  <div class="news-list">
    <div v-for="item in news" :key="item.id" class="news-item">
      <h3>{{ item.title }}</h3>
      <p>{{ item.content }}</p>
      <div class="meta">
        <span>{{ item.date }}</span>
        <span>{{ item.author }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      news: []
    }
  },
  mounted() {
    this.fetchNews()
  },
  methods: {
    async fetchNews() {
      // 这里替换为实际的 API 调用
      // const response = await axios.get('/api/news')
      // this.news = response.data

      // 临时使用模拟数据
      this.news = [
        {
          id: 1,
          title: "Vue 3.0 正式发布",
          content: "Vue 3.0 带来了更快的性能和更好的 TypeScript 支持",
          date: "2023-01-01",
          author: "Evan You"
        },
        {
          id: 2,
          title: "Composition API 详解",
          content: "新的 Composition API 提供了更灵活的代码组织方式",
          date: "2023-01-02",
          author: "Vue 团队"
        }
      ]
    }
  }
}
</script>

<style scoped>
.news-list {
  max-width: 800px;
  margin: 0 auto;
}
.news-item {
  margin-bottom: 20px;
  padding: 15px;
  border: 1px solid #eee;
  border-radius: 4px;
}
.meta {
  color: #666;
  font-size: 0.8em;
  margin-top: 10px;
}
.meta span {
  margin-right: 10px;
}
</style>

使用 Composition API

如果需要使用 Vue 3 的 Composition API,可以这样改写:

vue实现新闻列表

<script setup>
import { ref, onMounted } from 'vue'

const news = ref([])

const fetchNews = async () => {
  // 实际项目中替换为 API 调用
  news.value = [
    {
      id: 1,
      title: "Vue 3.0 正式发布",
      content: "Vue 3.0 带来了更快的性能和更好的 TypeScript 支持",
      date: "2023-01-01",
      author: "Evan You"
    }
  ]
}

onMounted(() => {
  fetchNews()
})
</script>

分页功能

实现基本的分页功能可以添加以下代码:

<template>
  <!-- 原有代码不变 -->
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>第 {{ currentPage }} 页</span>
    <button @click="nextPage">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      news: [],
      currentPage: 1,
      totalPages: 1
    }
  },
  methods: {
    async fetchNews() {
      // 模拟分页数据
      const allNews = [...]
      const perPage = 10
      this.totalPages = Math.ceil(allNews.length / perPage)
      this.news = allNews.slice(
        (this.currentPage - 1) * perPage,
        this.currentPage * perPage
      )
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
        this.fetchNews()
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.fetchNews()
      }
    }
  }
}
</script>

路由跳转

为新闻项添加详情页跳转:

<template>
  <div class="news-list">
    <div 
      v-for="item in news" 
      :key="item.id" 
      class="news-item"
      @click="goToDetail(item.id)"
    >
      <!-- 原有内容 -->
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    goToDetail(id) {
      this.$router.push(`/news/${id}`)
    }
  }
}
</script>

以上代码提供了实现新闻列表的基本框架,可以根据实际需求进行调整和扩展。

标签: 列表新闻
分享给朋友:

相关文章

vue实现列表权限

vue实现列表权限

实现列表权限的基本思路 在Vue中实现列表权限通常涉及根据用户角色或权限动态渲染列表内容。核心逻辑是通过权限判断控制列表项的显示、隐藏或操作权限。 权限数据管理 使用Vuex或Pinia存储全局权限…

vue实现列表

vue实现列表

实现列表的基本方法 在Vue中实现列表通常使用v-for指令,这是Vue的核心功能之一。v-for可以遍历数组或对象,为每个元素生成对应的DOM节点。 <template> <…

vue实现悬浮列表

vue实现悬浮列表

Vue 实现悬浮列表的方法 使用 CSS 固定定位 通过 CSS 的 position: fixed 属性实现悬浮效果。在 Vue 模板中,为列表容器添加固定定位样式,并设置 top、left 等属…

vue实现文本列表

vue实现文本列表

Vue 实现文本列表的方法 Vue 可以通过多种方式实现文本列表的渲染和管理,以下是几种常见的方法: 使用 v-for 指令渲染列表 通过 v-for 指令可以动态渲染数组或对象中的文本列表。 &…

vue实现列表全选

vue实现列表全选

实现全选功能的基本思路 在Vue中实现列表全选功能,通常需要结合v-model和计算属性来管理选中状态。全选按钮的状态与列表中所有项的选中状态相互关联。 基础实现方法 准备一个数据列表和对应的选中状…

vue实现列表组件

vue实现列表组件

Vue 列表组件实现方法 基础列表渲染 使用 v-for 指令渲染数组数据,需配合 :key 提升性能: <template> <ul> <li v-for…