cnpm install view-design --save
2,引入ViewUI。
// import iView from 'iview';
// import 'iview/dist/styles/iview.css';
import ViewUI from 'view-design';
import 'view-design/dist/styles/iview.css';
//Vue.use(iView)
Vue.use(ViewUI);
3,编写代码
export default {
data() {
return {
//defaultList:{name:name,url:url}格式,是默认的已上传图片列表,此处不需要便去了,不然会显示框框可难看
defaultList: [
],
imgName: '',
imgUrl:'',
visible: false,
uploadList: []
}
},
methods: {
handleView(name) {
this.imgName = name;
this.visible = true;
},
handleRemove(file) {
const fileList = this.$refs.upload.fileList;
this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
},
handleSuccess(res, file) {
console.log(res.data)
file.url=res.data.imgUrl
file.name=res.data.imgUrl
this.imgUrl=res.data.imgUrl
},
handleFormatError(f服务器托管网ile) {
this.$Notice.warning({
title: 'The file format is incorrect',
desc: 'File format of ' + file.name + ' is incorrect, please select jpg or png.'
});
},
handleMaxSize(file) {
this.$Notice.warning({
title: 'Exceeding file size limit',
desc: 'File ' + file.name + ' is too large, no more than 2M.'
});
},
handleBeforeUpl服务器托管网oad() {
const check = this.uploadList.length < 5;
if (!check) {
this.$Notice.warning({
title: 'Up to five pictures can be uploaded.'
});
}
return check;
}
},
mounted() {
this.uploadList = this.$refs.upload.fileList;
}
}
.demo-upload-list {
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
line-height: 60px;
border: 1px solid transparent;
border-radius: 4px;
overflow: hidden;
background: #fff;
position: relative;
box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
margin-right: 4px;
}
.demo-upload-list img {
width: 100%;
height: 100%;
}
.demo-upload-list-cover {
display: none;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, .6);
}
.demo-upload-list:hover .demo-upload-list-cover {
display: block;
}
.demo-upload-list-cover i {
color: #fff;
font-size: 20px;
cursor: pointer;
margin: 0 2px;
}
4,需要注意的地方
4.1,template下加个div,否则多个根元素会报错
4.2,其余代码格式结构保持不变,div红框代表已上传的图片列表展示,里面的两个handleView是预览handleRemove是删除。on-remove钩子函数测试发现无效,可用此处handleRemove代替
4.3,upload中请添加name属性(如果需要),action替换成自己的接口地址
4.4,Modal中替换:src图片地址
4.5,:on-success钩子函数请将上传成功的图片地址和名称赋值给file.url和file.name
本代码上传前默认效果与上传后效果
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: 从兔子说起:了解RabbitMQ消息的多样化【RabbitMQ 二】
:你只管努力,剩下的交给时间 :小破站 从兔子说起:了解RabbitMQ消息的多样化 第一:消息的可靠性与持久性 第二:消息交换机与队列 第三:消息确认与拒绝 第四:消息的过期与优先级 第五:消息顺序保证 第一:消息的可靠性与持久性 在消息传递系统中,确保消息…