1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <template>
- <div class="component-upload-image">
- <el-upload
- :action="uploadImgUrl"
- list-type="picture-card"
- :on-success="handleUploadSuccess"
- :before-upload="handleBeforeUpload"
- :on-error="handleUploadError"
- name="file"
- :show-file-list="false"
- :headers="headers"
- style="display: inline-block; vertical-align: top"
- >
- <el-image v-if="!value" :src="value">
- <div slot="error" class="image-slot">
- <i class="el-icon-plus" />
- </div>
- </el-image>
- <div v-else class="image">
- <el-image :src="value" />
- <div class="mask">
- <div class="actions">
- <span title="移除" @click.stop="removeImage">
- <i class="el-icon-delete" />
- </span>
- </div>
- </div>
- </div>
- </el-upload>
- </div>
- </template>
- <script>
- import { getToken } from "@/utils/auth";
- export default {
- components: {},
- data() {
- return {
- uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
- headers: {
- Authorization: "Bearer " + getToken(),
- },
- };
- },
- props: {
- value: {
- type: String,
- default: "",
- },
- },
- methods: {
- removeImage() {
- this.$emit("input", "");
- },
- handleUploadSuccess(res) {
- this.$emit("input", res.url);
- this.loading.close();
- },
- handleBeforeUpload() {
- this.loading = this.$loading({
- lock: true,
- text: "上传中",
- background: "rgba(0, 0, 0, 0.7)",
- });
- },
- handleUploadError() {
- this.$message({
- type: "error",
- message: "上传失败",
- });
- this.loading.close();
- },
- },
- watch: {},
- };
- </script>
- <style scoped lang="scss">
- .avatar {
- width: 100%;
- height: 100%;
- }
- .image {
- position: relative;
- .mask {
- opacity: 0;
- position: absolute;
- top: 0;
- width: 100%;
- background-color: rgba(0, 0, 0, 0.5);
- transition: all 0.3s;
- }
- &:hover .mask {
- opacity: 1;
- }
- }
- </style>
|