当前位置:首页 > 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>

处理数据与交互逻辑

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

<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>

添加样式优化体验

为组件添加基础样式:

<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 中实现扫描二维码功能,可以通过第三方库或原生 API 完成。以下是两种常见方法: 使用 vue-qrcode-reader 库 vue-qrcode-re…

vue 实现手册

vue 实现手册

以下是关于 Vue.js 实现手册的整理内容,涵盖核心概念、实践方法和常见场景的解决方案: 核心概念与基础用法 Vue.js 是一个渐进式 JavaScript 框架,核心特点包括数据驱动视图和组…

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件可…

vue点击实现排序

vue点击实现排序

实现点击排序的方法 在Vue中实现点击排序功能,可以通过以下几种方式完成: 使用计算属性结合排序方法 定义一个响应式数据存储排序状态,通过计算属性动态返回排序后的数组。点击事件切换排序状态。…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const t…