0
点赞
收藏
分享

微信扫一扫

创作者前端-头像上传前端

添加缩略图,和上传图片的组件,上传图片的组件我是从 vue-element-admin 中进行拷贝过来的,如果你想使用和我不同的上传组件可以自行去弄一个,我的话就使用 vue-element-admin 提供的了,下载 vue-element-admin

下载地址:​​https://github.com/PanJiaChen/vue-element-admin​​

创作者前端-头像上传前端_Project

创作者前端-头像上传前端_上传图片_02

在添加创作者页面当中导入该组件

创作者前端-头像上传前端_上传图片_03

import imageCropper from "@/components/ImageCropper";
import panThumb from "@/components/PanThumb";

components: {imageCropper, panThumb},

修改添加页面,添加缩略图

创作者前端-头像上传前端_上传_04

<el-form-item>
  <pan-thumb :width="String('100px')" :height="String('100px')" :image="String(author.avatar)"/>
</el-form-item>

上传文件

上传界面

创作者前端-头像上传前端_上传_05

<el-form-item>
  <pan-thumb :width="String('100px')" :height="String('100px')" :image="String(author.avatar)"/>

  <el-button type="primary" @click="imageCropperShow=true">上传头像</el-button>
  <!--
  上传头像的界面
  -->
  <image-cropper
    v-show="imageCropperShow"
    :width="500"
    :height="300"
    :key="cropperKey"
    :url="BASE_API+'/service_upload/file/uploadOssFile'"
    field="file"
    @close="close"
    @crop-upload-success="cropSuccess"
  />
</el-form-item>

事件处理

创作者前端-头像上传前端_上传图片_06

data()&nbsp;{
&nbsp;&nbsp;return&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;author:&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 排序默认值
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort:&nbsp;0,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level:&nbsp;1
&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;imageCropperShow:&nbsp;false,
&nbsp;&nbsp;&nbsp;&nbsp;cropperKey:&nbsp;0,
&nbsp;&nbsp;&nbsp;&nbsp;BASE_API:&nbsp;process.env.VUE_APP_BASE_API
&nbsp;&nbsp;}
},

创作者前端-头像上传前端_上传图片_07

close()&nbsp;{
&nbsp;&nbsp;// 关闭弹框
&nbsp;&nbsp;this.imageCropperShow&nbsp;=&nbsp;false;
&nbsp;&nbsp;// 该代码可以清空上传组件的默认样式,也就是说每次弹出来的窗口都是新的
&nbsp;&nbsp;this.cropperKey&nbsp;=&nbsp;this.cropperKey&nbsp;+&nbsp;1;
},
cropSuccess(data)&nbsp;{
&nbsp;&nbsp;// 关闭弹框
&nbsp;&nbsp;this.imageCropperShow&nbsp;=&nbsp;false;
&nbsp;&nbsp;// 显示图片
&nbsp;&nbsp;this.author.avatar&nbsp;=&nbsp;data.url;

&nbsp;&nbsp;this.cropperKey&nbsp;=&nbsp;this.cropperKey&nbsp;+&nbsp;1;
}

如上内容都完成好了之后,测试上传效果图如下所示

创作者前端-头像上传前端_上传图片_08


举报

相关推荐

0 条评论