当前位置:首页 > 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实现WiFi列表

vue实现WiFi列表

获取WiFi列表的Vue实现 使用Vue获取WiFi列表需要结合浏览器API或后端服务,因为浏览器出于安全限制无法直接访问系统WiFi信息。以下是两种常见实现方式: 方法一:通过Web API获取附…

vue实现列表水印

vue实现列表水印

实现列表水印的方法 在Vue中为列表添加水印可以通过多种方式实现,以下是几种常见的方法: 使用CSS背景图 通过CSS的background-image属性为列表元素添加水印背景。水印可以是文字或…

vue列表查询实现

vue列表查询实现

实现Vue列表查询功能 基本数据绑定与渲染 在Vue中实现列表查询,首先需要定义数据源和查询条件。通过v-model绑定搜索输入框,使用计算属性过滤列表。 <template> &l…

vue实现列表添加

vue实现列表添加

使用 Vue 实现列表添加功能 数据绑定与列表渲染 在 Vue 中,通过 v-for 指令可以轻松实现列表渲染。首先需要定义一个数组来存储列表数据,并在模板中使用 v-for 循环渲染。 <…

uniapp 内容列表

uniapp 内容列表

uniapp 内容列表的实现方法 在uniapp中实现内容列表通常需要结合组件和数据绑定。以下是一个常见的方法: 模板部分 <template> <view class=…

vue实现列表全选

vue实现列表全选

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