VUE哔哩哔哩移动端项目使用vant 实现发布功能(上传文件)

本文主要是介绍VUE哔哩哔哩移动端项目使用vant 实现发布功能(上传文件),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

bilibili发布页面主要有:发布按钮、多行文本框、一个上传图片的按钮(此处使用了vant)

在这里插入图片描述

组件里面的头部有:左返回按钮(使用vant)、发布按钮

<div class="top"><van-icon name="arrow-left" class="top-lef" @click="go" /><!-- 给按钮绑定一个class 如果computed返回的是true 就调用rel-highlight 这个css样式--><divclass="rel":class="{ 'rel-highlight': isHighLight }"@click="regSave">发布</div></div>

需要实现功能:当文本框输入内容时 发布按钮就会高亮。

实现思路:使用computed监听文本框的变化,当文本框的内容长度>0时,就调用isHighLight函数,动态改变按钮的class名(rel-highlight),即改变了按钮的样式 。
<div class="text"><textareaclass="text"placeholder="分享我的哔哩哔哩动态"v-model.trim="msg"></textarea></div>

v-model.trim=“msg” 给文本框的内容进行双向绑定 同时删除前后空格 (当输入空格时 发布按钮的事件并不会触发 不会高亮)

computed: {isHighLight: function () {// 判断文本框是否输入内容 有内容是true 就调用高亮函数return this.msg.length > 0;},},
上传文件:此处使用了vant

在这里插入图片描述

在这里插入图片描述

想要实现的效果就是这样的 但是当时出现了一个问题,就是我向后端发送请求之后 图片预览不出来。 (vant官方图片之所以能预览出来,是因为它自动将图片发送到vant的服务器,所以能展示出来。)
解决方法:查找vant文件上传的官方函数有:before-read=“beforeRead” 是上传前置处理,类似于钩子函数,在图片上传到vant服务器之前,将图片/视频的地址发到后端的服务器,如果上传成功,后端把拿到图片的地址和文件类型返回过来,图片/视频就可以展示出来。
:after-read=“afterRead” : 文件上传完毕后会触发 after-read 回调函数,获取到对应的 file 对象,将图片展示出来。

  <!-- 上传的图片预览 --><div class="add"><div v-for="(i, idx) in fileMe" :key="idx" class="a-showImgVideo">//当上传的文件类型为0时 就显示图片;是1时就显示视频<img class="myImg" :src="i.Mysrc" v-if="i.type == '0'" /><videoclass="myImg"width="320"height="240"v-if="i.type == '1'"autoplaymuted><source :src="i.Mysrc" type="video/mp4" /></video></div><!-- vant 上传图片/视频 --><van-uploaderv-model="fileList"multiple:before-read="beforeRead"/></div>

调用 beforeRead函数,将文件的地址和类型传送到后端服务器 成功之后拿到返回值,显示出来:

methods: {beforeRead(file) {console.log("file", file);console.log("file.type", file.type);//判断文件的类型是否是图片let type = file.type.indexOf("image") == -1 ? 1 : 0;//使用FormData 的最大优点就是可以异步上传二进制文件const forms = new FormData();forms.append("filename", file); // 增加文件的参数forms.append("type", type); // 增加类型参数//获取返回过来的图片/视频 进行展示axios({url: "/bili/dynamic/uploadPicture",method: "post",data: forms, //URLSearchParams对象用于处理URL中查询字符串,即?之后的部分。headers: {"Content-Type": "multipart/form-data",}, //php 默认要求传递请求头}).then((res) => {console.log("返回res.data", res.data);// if (res.data.data.code == "0") {// if(res.data.code == 0){console.log("上传成功", res.data.msg);let imgInfo = {Mysrc: res.data.url,type: res.data.type,};this.fileMe.push(imgInfo);//fileMe是在data里定义的 是个空的数组 存的是上传图片的地址和类型console.log("this.imgUrl", this.imgUrl);}).catch((e) => {console.log("服务器出错", e);});},
点击发布按钮之后 将文本和图片/视频上传到后端服务器
// 点击发布按钮regSave() {console.log("this.fileMe", this.fileMe);// 通过FormData将文件转成二进制数据const forms = new FormData();forms.append("uid", sessionStorage.getItem("uid")); //  uidforms.append("urlList", [this.fileMe[0].Mysrc]); // 添加 键和值forms.append("descr", this.msg); // 获取上传图片描述// forms.append("date", this.fileDate); // 获取上传图片上传时间forms.append("type", this.fileMe[0].type); // 获取上传图片类型axios({url: "/bili/dynamic/addDynamic",method: "post",data: forms, //URLSearchParams类型headers: {"Content-Type": "multipart/form-data",}, //php 默认要求传递请求头}).then((res) => {// if (res.data.status === 200) {console.log(res.data);}).catch((e) => {console.log("服务器出错了", e);});setTimeout(() => {// 发布成功 跳转到动态页面this.$router.push('/Move/Space/SpaceMy');}, 2000);},

全部代码:

<template><div class="box"><div class="top"><van-icon name="arrow-left" class="top-lef" @click="go" /><!-- 给按钮绑定一个class 如果computed返回的是true 就调用rel-highlight 这个css样式--><divclass="rel":class="{ 'rel-highlight': isHighLight }"@click="regSave">发布</div></div><van-notice-bar mode="link">新人福利,分享视频领限定头像框、个性装扮</van-notice-bar><div class="content"><div class="text"><textareaclass="text"placeholder="分享我的哔哩哔哩动态"v-model.trim="msg"></textarea></div><!-- 上传的图片预览 --><div class="add"><div v-for="(i, idx) in fileMe" :key="idx" class="a-showImgVideo"><img class="myImg" :src="i.Mysrc" v-if="i.type == '0'" /><videoclass="myImg"width="320"height="240"v-if="i.type == '1'"autoplaymuted><source :src="i.Mysrc" type="video/mp4" /></video></div><!-- 上传图片/视频 --><van-uploaderv-model="fileList"multiple:before-read="beforeRead"/></div><div class="address" v-show="isShow"><div class="address-lef"><span class="iconfont-zm icon-lujing"></span>西安市</div><div class="address-rig"><span class="iconfont-zm icon-X" @click="isShow = false"></span></div></div></div></div>
</template><script>
import axios from "axios";export default {name: "RelCenter",data() {return {// 用户输入  msgmsg: "",url: "",fileMe: [],isShow: true,fileList: [// { url: "https://img01.yzcdn.cn/vant/leaf.jpg" },// Uploader 根据文件后缀来判断是否为图片文件// 如果图片 URL 中不包含类型信息,可以添加 isImage 标记来声明],};},methods: {beforeRead(file) {console.log("file", file);console.log("file.type", file.type);//判断文件类型是不是图片let type = file.type.indexOf("image") == -1 ? 1 : 0;const forms = new FormData();forms.append("filename", file); // 获取上传图片信息forms.append("type", type); // 获取上传图片信息console.log("type shiping", type);axios({url: "/bili/dynamic/uploadPicture",method: "post",data: forms, //URLSearchParams类型headers: {"Content-Type": "multipart/form-data",}, //php 默认要求传递请求头}).then((res) => {console.log("返回res.data", res.data);// if (res.data.data.code == "0") {// if(res.data.code == 0){console.log("上传成功", res.data.msg);let imgInfo = {Mysrc: res.data.url,type: res.data.type,};this.fileMe.push(imgInfo);console.log("this.imgUrl", this.imgUrl);}).catch((e) => {console.log("服务器出错", e);});},go() {this.$router.go(-1);},// 点击发布按钮regSave() {console.log("this.fileMe", this.fileMe);// 通过FormData将文件转成二进制数据const forms = new FormData();forms.append("uid", sessionStorage.getItem("uid")); //  uidforms.append("urlList", [this.fileMe[0].Mysrc]); // 添加 键和值forms.append("descr", this.msg); // 获取上传图片描述// forms.append("date", this.fileDate); // 获取上传图片上传时间forms.append("type", this.fileMe[0].type); // 获取上传图片类型axios({url: "/bili/dynamic/addDynamic",method: "post",data: forms, //URLSearchParams类型headers: {"Content-Type": "multipart/form-data",}, //php 默认要求传递请求头}).then((res) => {// if (res.data.status === 200) {console.log(res.data);}).catch((e) => {console.log("服务器出错了", e);});setTimeout(() => {// 跳转到动态页面this.$router.push('/Move/Space/SpaceMy');}, 2000);},},computed: {isHighLight: function () {// 判断文本框是否输入内容 有内容是true 就调用高亮函数return this.msg.length > 0;},},
};
</script><style scoped>
@import url(../../assets/IconFont/Moves/font3/iconfont.css);
.box {width: 100%;
}.top {width: 100%;height: 0.44rem;line-height: 0.44rem;display: flex;justify-content: space-between;align-items: center;padding: 0 0.16rem;box-sizing: border-box;
}
.top-lef {font-size: 0.16rem;color: #000;
}
.rel {width: 0.5rem;height: 0.28rem;background-color: #e7e7e7;border-radius: 0.14rem;font-size: 0.14rem;text-align: center;line-height: 0.28rem;color: #919191;
}.content {width: 100%;
}
.text {width: 100%;height: 1.3rem;box-sizing: border-box;padding: 0.2rem 0.16rem 0;
}
textarea {width: 100%;height: 1.3rem;color: #000;font-size: 0.16rem;border: 0;outline: none;
}
.add {width: 1.14rem;height: 1.14rem;padding: 0 0.16rem;
}.van-uploader__upload {width: 1.14rem;height: 1.14rem;border: 0.01rem solid dashed;
}
.address {width: 1.05rem;height: 0.26rem;line-height: 0.26rem;margin-left: 0.12rem;color: #277ac3;border-radius: 0.13rem;margin-top: 0.7rem;position: fixed;bottom: 0.6rem;
}
.address-lef {float: left;width: 0.77rem;height: 0.26rem;text-align: center;color: #277ac3;background-color: #f4f4f4;border-top-left-radius: 0.14rem;border-bottom-left-radius: 0.14rem;box-sizing: border-box;
}
.address-rig {float: left;width: 0.26rem;height: 0.26rem;line-height: 0.26rem;text-align: center;color: #277ac3;margin-left: 0.02rem;background-color: #f4f4f4;border-top-right-radius: 0.14rem;border-bottom-right-radius: 0.14rem;
}
.icon-lujing {font-size: 0.14rem;margin-right: 0.04rem;
}
.icon-X {font-size: 0.08rem;
}
//上传成功之后图片和视频的大盒子样式
.a-showImgVideo {margin-top: 0.2rem;width: 1.2rem;height: 1.2rem;
}
.a-showImgVideo .myImg {width: 100%;height: 100%;
}
</style>
<style>
.preview-cover {position: absolute;bottom: 0;box-sizing: border-box;width: 100%;padding: 0.04rem;color: #fff;font-size: 0.12rem;text-align: center;background: rgba(0, 0, 0, 0.3);
}
.van-uploader .van-uploader__wrapper {flex-wrap: nowrap;
}/* 动态样式 */
.top .rel-highlight {background-color: #ea7a99;color: #fff;
}
</style>

这篇关于VUE哔哩哔哩移动端项目使用vant 实现发布功能(上传文件)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/834897

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。