当前位置:首页 > VUE

vue文章列表详情实现

2026-01-23 12:05:45VUE

实现Vue文章列表与详情功能

技术栈建议

  • Vue 2/3 + Vue Router
  • 状态管理:Vuex/Pinia(可选)
  • UI库:Element UI/Ant Design Vue(可选)

基础项目结构

src/
├── views/
│   ├── ArticleList.vue  # 列表页
│   └── ArticleDetail.vue # 详情页
├── router/
│   └── index.js        # 路由配置
├── components/
│   └── ArticleCard.vue # 文章卡片组件

路由配置示例

// router/index.js
const routes = [
  {
    path: '/articles',
    component: () => import('../views/ArticleList.vue')
  },
  {
    path: '/articles/:id',
    component: () => import('../views/ArticleDetail.vue'),
    props: true
  }
]

文章列表实现

<!-- ArticleList.vue -->
<template>
  <div class="article-list">
    <ArticleCard 
      v-for="article in articles" 
      :key="article.id"
      :article="article"
      @click="goToDetail(article.id)"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      articles: [
        { id: 1, title: 'Vue基础', excerpt: 'Vue入门教程...' },
        { id: 2, title: 'Vue Router', excerpt: '路由管理指南...' }
      ]
    }
  },
  methods: {
    goToDetail(id) {
      this.$router.push(`/articles/${id}`)
    }
  }
}
</script>

文章详情实现

<!-- ArticleDetail.vue -->
<template>
  <div v-if="article">
    <h1>{{ article.title }}</h1>
    <div class="content">{{ article.content }}</div>
  </div>
</template>

<script>
export default {
  props: ['id'],
  data() {
    return {
      article: null
    }
  },
  created() {
    this.fetchArticle()
  },
  methods: {
    fetchArticle() {
      // 模拟API请求
      const articles = [
        { 
          id: 1, 
          title: 'Vue基础', 
          content: '这里是详细内容...' 
        }
      ]
      this.article = articles.find(a => a.id === Number(this.id))
    }
  }
}
</script>

进阶优化方案

API数据获取

// 使用axios示例
import axios from 'axios'

export default {
  methods: {
    async fetchArticles() {
      try {
        const res = await axios.get('/api/articles')
        this.articles = res.data
      } catch (error) {
        console.error(error)
      }
    }
  }
}

状态管理(Pinia示例)

// stores/article.js
import { defineStore } from 'pinia'

export const useArticleStore = defineStore('article', {
  state: () => ({
    articles: [],
    currentArticle: null
  }),
  actions: {
    async fetchAll() {
      const res = await axios.get('/api/articles')
      this.articles = res.data
    },
    async fetchOne(id) {
      const res = await axios.get(`/api/articles/${id}`)
      this.currentArticle = res.data
    }
  }
})

列表分页实现

<template>
  <div>
    <!-- 列表内容 -->
    <el-pagination
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page
      this.fetchArticles()
    },
    async fetchArticles() {
      const res = await axios.get('/api/articles', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      })
      this.articles = res.data.items
      this.total = res.data.total
    }
  }
}
</script>

样式建议

/* 列表样式 */
.article-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

/* 详情页样式 */
.article-content {
  line-height: 1.6;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

vue文章列表详情实现

分享给朋友:

相关文章

Vue实现商品详情

Vue实现商品详情

Vue实现商品详情页的方法 数据获取与渲染 使用Vue的data属性存储商品详情数据,通过v-for指令循环渲染商品属性列表。结合v-if或v-show控制图片预览、库存状态等动态显示。 data(…

vue实现文章列表

vue实现文章列表

Vue 实现文章列表 数据准备 在 Vue 中实现文章列表,通常需要一个数组存储文章数据。可以在组件的 data 中定义文章列表数据,或通过 API 从后端获取。 data() { return…

vue实现商品详情讲解

vue实现商品详情讲解

Vue 实现商品详情页的核心步骤 搭建基础结构 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 vue-router、axios)。通过路由配置商品详情页路径(例如 /product…

vue实现查询详情

vue实现查询详情

Vue 实现查询详情功能 查询详情功能通常涉及从列表页跳转到详情页,并显示对应项的详细信息。以下是实现这一功能的几种方法: 使用路由参数传递 ID 在路由配置中定义动态参数,通过 this.$…

vue怎么实现跳转详情

vue怎么实现跳转详情

vue实现页面跳转详情的方法 在Vue中实现页面跳转详情通常可以通过以下几种方式实现: 1. 使用<router-link>组件 通过Vue Router提供的<router-l…

vue实现商品详情功能

vue实现商品详情功能

商品详情页基础结构 使用Vue单文件组件构建商品详情页的基本框架,包含标题、图片轮播、价格、规格选择等核心区域: <template> <div class="product-…