当前位置:首页 > VUE

vue文章列表详情实现

2026-02-24 02:42:03VUE

Vue 文章列表与详情页实现

路由配置

router/index.js 中配置动态路由,实现列表到详页的跳转:

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

文章列表组件

使用 v-for 渲染列表数据,通过 router-link 实现跳转:

vue文章列表详情实现

<template>
  <div>
    <div v-for="article in articles" :key="article.id">
      <router-link :to="'/articles/' + article.id">
        <h3>{{ article.title }}</h3>
      </router-link>
      <p>{{ article.summary }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      articles: [
        { id: 1, title: 'Vue入门', summary: 'Vue基础知识介绍' },
        { id: 2, title: 'Vue路由', summary: 'Vue Router使用指南' }
      ]
    }
  }
}
</script>

文章详情组件

通过路由参数 id 获取对应文章内容:

<template>
  <div v-if="article">
    <h2>{{ article.title }}</h2>
    <div>{{ 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: '这里是详细内容...' },
        { id: 2, title: 'Vue路由', content: '这里是详细内容...' }
      ]
      this.article = articles.find(item => item.id === Number(this.id))
    }
  }
}
</script>

数据请求优化

使用 Axios 从后端 API 获取数据:

vue文章列表详情实现

import axios from 'axios'

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

状态管理(可选)

对于大型应用,可使用 Vuex 管理文章状态:

// store/modules/articles.js
const actions = {
  async fetchArticles({ commit }) {
    const res = await axios.get('/api/articles')
    commit('SET_ARTICLES', res.data)
  }
}

样式优化

添加过渡效果提升用户体验:

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}

分享给朋友:

相关文章

vue实现图片详情

vue实现图片详情

实现图片详情功能 在Vue中实现图片详情功能,可以通过点击图片放大查看或展示更多信息。以下是几种常见的实现方式: 使用模态框展示图片详情 创建一个模态框组件,点击图片时显示大图和相关描述信息。…

vue实现商品详情讲解

vue实现商品详情讲解

Vue 实现商品详情页的步骤 搭建基础结构 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 实现页面路由。商品详情页通常需要独立路由(如 /product/:id),通过动…

vue实现查询详情

vue实现查询详情

Vue 实现查询详情功能 基本实现思路 在 Vue 中实现查询详情功能通常涉及以下几个核心环节:路由配置、API 调用、数据绑定和页面渲染。以下是具体实现方法: 路由配置 在 Vue Router…

vue实现点击展开详情

vue实现点击展开详情

实现点击展开详情功能 在Vue中实现点击展开详情功能可以通过以下方法完成: 使用v-show或v-if控制显示隐藏 通过data中的布尔值变量控制详情内容的显示与隐藏: <template…

vue教程实现商品详情

vue教程实现商品详情

Vue 实现商品详情页教程 项目初始化 使用 Vue CLI 创建新项目,安装必要依赖如 vue-router 和 axios。 命令示例: vue create product-detail…

php实现详情页面分页

php实现详情页面分页

实现详情页面分页的方法 在PHP中实现详情页面的分页功能,可以通过以下步骤完成。假设详情页内容是长文本或需要分段展示的数据。 数据库准备 确保数据库表中有一个字段存储需要分页的长文本内容。例如:…