当前位置:首页 > VUE

vue实现微博发布动态

2026-01-07 06:10:33VUE

使用Vue实现微博发布动态功能

创建Vue组件结构

新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮:

<template>
  <div class="weibo-post">
    <textarea v-model="content" placeholder="分享你的新鲜事..."></textarea>
    <div class="preview-images">
      <div v-for="(img, index) in previewImages" :key="index" class="image-item">
        <img :src="img" />
        <button @click="removeImage(index)">×</button>
      </div>
    </div>
    <div class="actions">
      <input type="file" ref="fileInput" @change="handleImageUpload" multiple accept="image/*" />
      <button @click="triggerFileInput">添加图片</button>
      <button @click="postWeibo" :disabled="!content && !previewImages.length">发布</button>
    </div>
  </div>
</template>

处理数据与交互逻辑

在组件脚本部分实现核心功能:

vue实现微博发布动态

<script>
export default {
  data() {
    return {
      content: '',
      previewImages: [],
      files: []
    }
  },
  methods: {
    triggerFileInput() {
      this.$refs.fileInput.click()
    },
    handleImageUpload(e) {
      const files = e.target.files
      if (files.length + this.previewImages.length > 9) {
        alert('最多上传9张图片')
        return
      }

      Array.from(files).forEach(file => {
        if (!file.type.match('image.*')) return

        const reader = new FileReader()
        reader.onload = (e) => {
          this.previewImages.push(e.target.result)
          this.files.push(file)
        }
        reader.readAsDataURL(file)
      })
    },
    removeImage(index) {
      this.previewImages.splice(index, 1)
      this.files.splice(index, 1)
    },
    async postWeibo() {
      const formData = new FormData()
      formData.append('content', this.content)
      this.files.forEach(file => {
        formData.append('images', file)
      })

      try {
        const response = await axios.post('/api/weibo', formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
        this.$emit('posted', response.data)
        this.content = ''
        this.previewImages = []
        this.files = []
      } catch (error) {
        console.error('发布失败:', error)
      }
    }
  }
}
</script>

添加样式优化体验

为组件添加基础样式:

vue实现微博发布动态

<style scoped>
.weibo-post {
  border: 1px solid #e6e6e6;
  padding: 15px;
  border-radius: 4px;
  background: white;
}

textarea {
  width: 100%;
  min-height: 100px;
  border: none;
  resize: none;
  outline: none;
  font-size: 14px;
}

.actions {
  display: flex;
  align-items: center;
  margin-top: 10px;
}

input[type="file"] {
  display: none;
}

button {
  margin-right: 10px;
  padding: 5px 10px;
  background: #ff8200;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.preview-images {
  display: flex;
  flex-wrap: wrap;
  margin: 10px 0;
}

.image-item {
  position: relative;
  width: 100px;
  height: 100px;
  margin-right: 10px;
  margin-bottom: 10px;
}

.image-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-item button {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: red;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
}
</style>

后端API集成

在Vue项目中配置axios并创建API服务:

// src/api/weibo.js
import axios from 'axios'

export default {
  postWeibo(data) {
    return axios.post('/api/weibo', data)
  },
  getWeiboList(params) {
    return axios.get('/api/weibo', { params })
  }
}

使用组件

在父组件中引入并使用:

<template>
  <div>
    <weibo-post @posted="handlePosted" />
    <weibo-list :posts="posts" />
  </div>
</template>

<script>
import WeiboPost from './WeiboPost.vue'
import WeiboList from './WeiboList.vue'
import weiboApi from '@/api/weibo'

export default {
  components: { WeiboPost, WeiboList },
  data() {
    return {
      posts: []
    }
  },
  methods: {
    handlePosted(newPost) {
      this.posts.unshift(newPost)
    },
    fetchWeiboList() {
      weiboApi.getWeiboList().then(response => {
        this.posts = response.data
      })
    }
  },
  created() {
    this.fetchWeiboList()
  }
}
</script>

功能扩展建议

  • 添加@用户功能,使用正则匹配文本中的@符号
  • 实现话题标签功能,自动高亮#话题#
  • 添加表情选择器组件
  • 实现图片预览和放大功能
  • 添加字数限制和实时计数
  • 优化移动端体验和响应式布局

以上实现提供了微博发布动态的核心功能,可根据实际需求进行扩展和优化。

标签: 动态vue
分享给朋友:

相关文章

vue 实现菜单

vue 实现菜单

Vue 实现菜单的方法 使用 Vue 实现菜单可以通过多种方式,以下是几种常见的方法: 使用 Vue Router 实现动态路由菜单 通过 Vue Router 可以动态生成菜单,根据路由配置自动…

vue 实现弹幕

vue 实现弹幕

vue 实现弹幕的方法 使用 CSS 动画和动态渲染 在 Vue 中实现弹幕效果,可以通过动态渲染弹幕元素并结合 CSS 动画实现。以下是具体步骤: 数据准备 创建一个数组存储弹幕数据,每条弹幕…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景。…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现弹幕

vue实现弹幕

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

vue实现拖动

vue实现拖动

Vue实现拖动的常用方法 在Vue中实现拖动功能通常需要结合HTML5的拖放API或第三方库。以下是几种常见实现方式: 使用HTML5原生拖放API HTML5提供了原生拖放API,可以通过drag…