element UI 上传图片可拖拽预览图顺序

<template>
	<div>
	<el-upload
			:show-file-list="false"
			action='//up.qiniu.com/'
			list-type="picture-card"
			:data="upload_token"
			:before-upload="beforeAvatarUpload"
			:on-preview="handlePictureCardPreview"
			:on-remove="handleRemove"
			:on-success="uploadAppSuccess">
		<el-button size="small" type="primary">点击上传</el-button>
		<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
	</el-upload>
	<draggable
			class="list-group"
			tag="ul"
			v-model="allList"
			v-bind="dragOptions"
			@start="drag = true"
			@end="drag = false"
			
	>
		<!--<transition-group type="transition" :name="!drag ? 'flip-list' : null">-->
		<div v-loading="loadingapp" class="img-wrapper divBox" v-for="(item,index) in allList" >
			<img :src="item.url" v-if="item.url" width="148" height="148" class="imgSty">
			<i class="el-icon-delete delIcon" @click="deleImg(item.URL,index)"></i>
			<!--<img class="img-big" :src="imgUrl" v-else width="362" height="272">-->
		</div>
		<!--</transition-group>-->
	</draggable>
	<router-view></router-view>
	</div>
	
</template>
<style scoped>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}
	.divBox{
		/*flex-direction: row;*/
		float: left;
		position: relative;
		
	}
	.imgSty{
		margin-left: 3px;
	}
	.delIcon{
		position: absolute;
		top: 0;
		right: 0;
		font-size: 50px;
	}
	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}
	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}
	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>

<script>
  import draggable from "vuedraggable";
  import uploader from '../../components/Uploader'
  import {addOperateLanding, updateOperateLanding, QINIU_DOMAIN,uploadToken} from '../../api/api'
  export default {
    name: "transition-example-2",
    display: "Transitions",
    order: 7,
    components: {
      draggable,
      uploader
    },
    data() {
      return {
        loadingapp:false,
        allList:[],
        upload_token: {},
        imgUrl: require('../../assets/images/header_holder.png'),
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],
        // list: message.map((name, index) => {
        //   return { name, order: index + 1 };
        // }),
        drag: false
      };
    },
    methods: {
      //删除图片
      deleImg(data,index){
        console.log(data)
        console.log(index)
		this.allList.splice(index,1)
	  },
      //上传图片相关
      beforeAvatarUpload (file) {
        console.log('file:', file)

        this.$emit('onLoading', true)
        let that = this
        return new Promise(function (resolve, reject) {
          uploadToken()
            .then(res => {
              that.upload_token = {'token': res.data.uptoken}
              if (that.channelMsg) {
                that.upload_token = {'token': res.data.uptoken, key: QINIU_DOMAIN_APK + that.channelMsg + '/' + file.name}
              }
              resolve(true)
            })
            .catch(error => {
              console.log(JSON.stringify(error))

              that.$emit('onFailure', '上传图片失败')
              that.$emit('onLoading', false)
              reject(false)
            })
        })
      },

      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
        console.log('hahah',this.dialogImageUrl)
      },
      uploadAppSuccess(file, fileList){
        console.log('上传成功')
        console.log(file);
        console.log( fileList);
        console.log(QINIU_DOMAIN + '/' + file.hash)
		var temp = {
          url:QINIU_DOMAIN + '/' + file.hash,
		  URL:file.hash
		  
		}
        this.allList.push(temp)
//启动拖拽功能

      },
      sort() {
        this.list = this.list.sort((a, b) => a.order - b.order);
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
	  
	  
	  
    },
    computed: {
      dragOptions() {
        return {
          animation: 200,
          group: "description",
          disabled: false,
          ghostClass: "ghost"
        };
      }
    }
  }
</script>
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: 在Vue中使用Element UI图片可以按照以下步骤进行: 1. 首先,在项目中安装Element UI和axios: ``` npm install element-ui axios --save ``` 2. 在main.js中引入Element UI和axios,并使用: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import axios from 'axios' Vue.use(ElementUI) Vue.prototype.$axios = axios ``` 3. 在组件中使用`<el-upload>`组件进行文件上,并使用`<el-image>`组件进行: ```html <template> <div> <el-upload action="/api/upload" :on-success="handleSuccess" :on-preview="handlePreview" list-type="picture-card"> <i class="el-icon-plus"></i> </el-upload> <el-dialog :visible.sync="dialogVisible"> <el-image :src="imageUrl" :preview-src-list="previewImages"></el-image> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, imageUrl: '', previewImages: [] }; }, methods: { handleSuccess(response) { if (response.status === 200) { this.imageUrl = response.data.url; this.previewImages.push(response.data.url); } }, handlePreview(file) { this.dialogVisible = true; this.imageUrl = file.url; this.previewImages = [file.url]; } } } </script> ``` 4. 在服务器端设置相应的文件上接口,例如使用Node.js和express: ```javascript const express = require('express'); const multer = require('multer'); const app = express(); const upload = multer({ dest: 'uploads/' }); app.post('/api/upload', upload.single('file'), (req, res) => { // 处理上文件并返回文件URL const file = req.file; const url = `http://example.com/${file.filename}`; res.send({ url }); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` 上述代码实现了一个简单的上图片的功能,具体可根据实际需求进行调整和扩展。 ### 回答2: 在Vue中使用Element UI图片可以按照以下步骤进行: 1. 首先,在项目中引入Element UI组件库,并在需要使用的组件中进行注册。 ```js import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 2. 创建一个上组件,并初始化需要的数据,如图片列表和上接口等。 ```vue <template> <div> <el-upload action="/your-upload-url" :on-success="handleSuccess" :file-list="fileList" :on-remove="handleRemove" multiple > <el-button size="small" type="primary"> 选择图片 </el-button> </el-upload> <el-image v-for="(file, index) in fileList" :key="index" :src="file.url" :preview-src-list="previewList" fit="cover" style="width: 100px; height: 100px;" ></el-image> </div> </template> <script> export default { data() { return { fileList: [], // 上的文件列表 previewList: [] // 图片列表 } }, methods: { handleSuccess(response, file, fileList) { // 上成功的回调函数 fileList[fileList.length - 1].url = URL.createObjectURL(file.raw) this.previewList = fileList.map(file => file.url) }, handleRemove(file, fileList) { // 上文件移除的回调函数 this.previewList = fileList.map(file => file.url) } } } </script> ``` 在上述代码中,el-upload组件负责上图片,action属性指定上图片的接口地址,on-success属性可以监听上成功的事件,并通过handleSuccess方法返回上成功后的处理方式。file-list属性用于展示上成功的文件列表,并且绑定了on-remove属性来处理上文件的移除事件。 el-image组件用于展示图片,v-for指令遍历渲染fileList数组中的每张图片,通过src属性绑定图片的地址,在handleSuccess方法中将上成功的文件通过URL.createObjectURL方法生成临时的URL作为图片的地址。同时,preview-src-list属性绑定了previewList数组,用于展示图片的列表。 这样,当用户选择图片并上成功后,页面将会展示上图片,并提供功能。 ### 回答3: 使用Element UI库实现图片的步骤如下: 1. 首先,在项目中引入Element UI和Vue插件。 ``` import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 2. 在Vue组件中,添加一个上组件和一个用于图片的容器。 ``` <template> <div> <!-- 图片组件 --> <el-upload class="upload-demo" action="/your-upload-api" <!-- 上图片的接口地址 --> :on-success="handleUploadSuccess" > <el-button size="small" type="primary">点击上</el-button> </el-upload> <!-- 图片容器 --> <div class="preview-container"> <img :src="imageUrl" v-if="imageUrl" alt="图片" /> </div> </div> </template> <script> export default { data() { return { imageUrl: '' // 图片的URL }; }, methods: { handleUploadSuccess(response) { // 上成功后,将返回的图片URL赋值给imageUrl this.imageUrl = response.data.imageUrl; } } }; </script> ``` 3. 完成上述代码后,当用户选择图片并上成功时,上接口会返回图片的URL。通过`handleUploadSuccess`方法将返回的URL赋值给`imageUrl`,然后在容器中显示图片即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值