当前位置:首页 > VUE

vue实现新闻

2026-02-10 00:59:06VUE

Vue实现新闻功能

使用Vue框架实现新闻功能通常涉及新闻列表展示、新闻详情页、分类筛选等核心模块。以下是具体实现方案:

新闻列表组件

创建NewsList.vue组件展示新闻列表:

<template>
  <div class="news-list">
    <div v-for="item in newsList" :key="item.id" class="news-item">
      <h3 @click="goDetail(item.id)">{{ item.title }}</h3>
      <p>{{ item.summary }}</p>
      <span>{{ item.createTime }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newsList: []
    }
  },
  mounted() {
    this.fetchNews()
  },
  methods: {
    fetchNews() {
      axios.get('/api/news').then(res => {
        this.newsList = res.data
      })
    },
    goDetail(id) {
      this.$router.push(`/news/${id}`)
    }
  }
}
</script>

新闻详情组件

创建NewsDetail.vue组件展示具体内容:

vue实现新闻

<template>
  <div v-if="news" class="news-detail">
    <h1>{{ news.title }}</h1>
    <div v-html="news.content"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      news: null
    }
  },
  created() {
    this.fetchNews(this.$route.params.id)
  },
  methods: {
    fetchNews(id) {
      axios.get(`/api/news/${id}`).then(res => {
        this.news = res.data
      })
    }
  }
}
</script>

路由配置

router/index.js中配置新闻相关路由:

{
  path: '/news',
  component: NewsList
},
{
  path: '/news/:id',
  component: NewsDetail
}

新闻分类筛选

添加分类筛选功能:

vue实现新闻

<template>
  <div>
    <select v-model="selectedCategory" @change="filterNews">
      <option value="">全部</option>
      <option v-for="cat in categories" :value="cat.id">{{ cat.name }}</option>
    </select>
    <NewsList :newsList="filteredNews" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedCategory: '',
      categories: [],
      allNews: []
    }
  },
  computed: {
    filteredNews() {
      return this.selectedCategory 
        ? this.allNews.filter(n => n.category === this.selectedCategory)
        : this.allNews
    }
  }
}
</script>

数据管理方案

对于复杂新闻系统,建议使用Vuex进行状态管理:

// store/modules/news.js
export default {
  state: {
    news: [],
    currentNews: null
  },
  mutations: {
    SET_NEWS(state, payload) {
      state.news = payload
    },
    SET_CURRENT_NEWS(state, payload) {
      state.currentNews = payload
    }
  },
  actions: {
    async fetchNews({ commit }) {
      const res = await axios.get('/api/news')
      commit('SET_NEWS', res.data)
    }
  }
}

无限滚动加载

实现新闻列表无限滚动:

<template>
  <div @scroll="handleScroll">
    <!-- 新闻列表内容 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      page: 1,
      isLoading: false
    }
  },
  methods: {
    handleScroll() {
      if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 100) {
        this.loadMore()
      }
    },
    loadMore() {
      if (this.isLoading) return
      this.isLoading = true
      axios.get(`/api/news?page=${this.page}`).then(res => {
        this.newsList.push(...res.data)
        this.page++
      }).finally(() => {
        this.isLoading = false
      })
    }
  }
}
</script>

注意事项

  1. 新闻内容安全:使用v-html渲染富文本时需防范XSS攻击,建议使用DOMPurify等库过滤
  2. SEO优化:如需搜索引擎收录,建议采用SSR方案(如Nuxt.js)
  3. 图片懒加载:新闻图片较多时建议使用v-lazy等懒加载技术
  4. 移动端适配:注意响应式设计,确保在移动设备上有良好体验

以上方案可根据实际项目需求进行组合和调整,核心思想是通过组件化方式构建新闻系统的各个功能模块。

标签: 新闻vue
分享给朋友:

相关文章

vue实现selectable

vue实现selectable

Vue 实现可选择的 Select 组件 在 Vue 中实现一个可选择的 Select 组件,可以通过以下几种方式: 使用原生 HTML select 元素 <template> &…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…

vue实现按钮

vue实现按钮

Vue 实现按钮的方法 使用原生 HTML 按钮 在 Vue 模板中可以直接使用 HTML 的 <button> 元素,通过 v-on 或 @ 绑定点击事件。 <template&…

vue博客实现

vue博客实现

Vue 博客实现步骤 项目初始化与基础配置 使用 Vue CLI 或 Vite 创建项目,安装基础依赖如 vue-router 和状态管理库(如 pinia)。配置路由文件实现页面跳转逻辑,例如博客首…