当前位置:首页 > VUE

vue实现微博话题

2026-02-09 13:44:49VUE

Vue 实现微博话题功能

微博话题功能通常包括话题的展示、点击跳转、热门话题推荐等模块。以下是基于 Vue 的实现方案:

数据格式设计

话题数据通常从后端获取,格式示例:

{
  "topics": [
    {
      "id": 1,
      "name": "#Vue3新特性#",
      "heat": 1200000,
      "link": "/topic/1"
    },
    {
      "id": 2,
      "name": "#前端开发#",
      "heat": 980000,
      "link": "/topic/2"
    }
  ]
}

基础话题组件实现

创建 Topic.vue 组件:

vue实现微博话题

<template>
  <div class="topic-list">
    <router-link 
      v-for="topic in topics" 
      :key="topic.id" 
      :to="topic.link"
      class="topic-item"
    >
      {{ topic.name }}
      <span class="heat" v-if="showHeat">({{ formatHeat(topic.heat) }})</span>
    </router-link>
  </div>
</template>

<script>
export default {
  props: {
    topics: {
      type: Array,
      required: true
    },
    showHeat: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    formatHeat(heat) {
      return heat > 10000 ? `${(heat/10000).toFixed(1)}万` : heat
    }
  }
}
</script>

<style scoped>
.topic-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.topic-item {
  color: var(--primary-color);
  background: #f2f2f2;
  padding: 4px 8px;
  border-radius: 4px;
  text-decoration: none;
}
.topic-item:hover {
  background: #e5e5e5;
}
.heat {
  color: #999;
  font-size: 0.8em;
}
</style>

话题输入功能

在发微博组件中添加话题识别:

<template>
  <textarea 
    v-model="content" 
    @input="detectTopics"
    placeholder="分享新鲜事..."
  />
  <div class="detected-topics" v-if="detectedTopics.length">
    检测到话题:
    <Topic :topics="detectedTopics" :showHeat="false"/>
  </div>
</template>

<script>
import Topic from './Topic.vue'

export default {
  components: { Topic },
  data() {
    return {
      content: '',
      detectedTopics: []
    }
  },
  methods: {
    detectTopics() {
      const topicRegex = /#[^#\s]+#/g
      const matches = this.content.match(topicRegex) || []
      this.detectedTopics = matches.map(name => ({
        name,
        link: `/topic/search?q=${encodeURIComponent(name)}`
      }))
    }
  }
}
</script>

热门话题推荐

创建热门话题组件 HotTopics.vue

vue实现微博话题

<template>
  <div class="hot-topics">
    <h3>热门话题</h3>
    <Topic :topics="hotTopics"/>
  </div>
</template>

<script>
import { fetchHotTopics } from '@/api/topics'
import Topic from './Topic.vue'

export default {
  components: { Topic },
  data() {
    return {
      hotTopics: []
    }
  },
  async created() {
    this.hotTopics = await fetchHotTopics()
  }
}
</script>

<style scoped>
.hot-topics {
  background: white;
  padding: 12px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
h3 {
  margin-top: 0;
  margin-bottom: 12px;
  font-size: 1.1em;
}
</style>

话题详情页

创建话题详情页 TopicDetail.vue

<template>
  <div v-if="topic">
    <h1>{{ topic.name }}</h1>
    <p>阅读量: {{ formatHeat(topic.heat) }}</p>
    <PostList :posts="posts"/>
  </div>
</template>

<script>
import { fetchTopicDetail } from '@/api/topics'
import PostList from '@/components/PostList.vue'

export default {
  components: { PostList },
  data() {
    return {
      topic: null,
      posts: []
    }
  },
  async created() {
    const topicId = this.$route.params.id
    const data = await fetchTopicDetail(topicId)
    this.topic = data.topic
    this.posts = data.posts
  },
  methods: {
    formatHeat(heat) {
      return heat > 10000 ? `${(heat/10000).toFixed(1)}万` : heat
    }
  }
}
</script>

路由配置

在 router.js 中添加路由:

{
  path: '/topic/:id',
  name: 'Topic',
  component: () => import('@/views/TopicDetail.vue')
}

实现要点

  • 使用正则表达式 /#[^#\s]+#/g 检测话题格式
  • 热度显示格式化为"万"单位
  • 话题样式使用浅色背景突出显示
  • 热门话题组件可复用基础话题组件
  • 话题详情页展示相关微博列表

以上实现可根据实际需求进行调整,如添加话题订阅功能、话题排行榜等扩展功能。

标签: 话题vue
分享给朋友:

相关文章

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

vue实现秒表

vue实现秒表

实现秒表功能 在Vue中实现秒表功能需要处理时间显示、开始、暂停和重置逻辑。以下是具体实现方式: 基础实现代码 <template> <div> <div&…