问题:一直显示token不存在?原因是没有设置请求头,所以要设置headers.
一、上传代码:
<el-upload class="avatar-uploader" action="http://43.128.40.9:8089/addssmin/fisdle/upload" //填写接口请求地址 :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" :headers="headers" //请求头 > <i class="el-icon-plus avatar-uploader-icon"></i> <!-- <img v-if="detailForm.imageUrl" :src="detailForm.imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> --> </el-upload>
二、js部分:
data(){ return{ headers:{"token": localStorage.getItem("token")}//本地获取 }, }, methods:{ handleAvatarSuccess(res, file) { this.imageUrl = URL.createObjectURL(file.raw); console.log("上传图片成功",res,file) }, beforeAvatarUpload(file) { const isJPG = file.type === 'image/jpeg'; const isLt2M = file.size / 1024 / 1024 < 2; console.log("beforeAvatarUpload",file) // if (!isJPG) { // this.$message.error('上传头像图片只能是 JPG 格式!'); // } // if (!isLt2M) { // this.$message.error('上传头像图片大小不能超过 2MB!'); // } // return isJPG && isLt2M; } }
从啥也不会开始吧