一、 图片上传
其实 EleUI 有现成的 上传图片 / 头像 ,自己用还是要改一下。
<el-upload class="avatar-uploader" action="uploadData" :http-request="UploadImage"
:show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="addimg" style="width: 100px;height: 100px;">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
handleAvatarSuccess(res, file) {
this.imageUrl = URL.createObjectURL(file.raw);
},
beforeAvatarUpload(file) {
const isJPG = file.type == 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上传图片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上传图片大小不能超过 2MB!');
}
return isJPG && isLt2M;
},
// 上传图片
UploadImage(param) {
let uploadData = new FormData();
uploadData.append('file', param.file);
uploadData.append('token', getToken('token'));
// 上传图片的接口 传上去后让后台返回一个地址
upload(uploadData).then((res) => {
// console.log(res)
if (res.code == 1) {
this.addimg = this.imgurl + res.data.url;
this.imageUrl = true;
}
}).catch((err) => {
console.log(err)
})
},
这种是只上传一张图片的方法,上传后台后 图片没有域名,是自己加的,写了一个公共的域名
Vue.prototype.imgurl = "xxxx"
后面要用的话就 this.imgurl
二、标准时间转成时间戳
时间呢就是用 EleUI 的 时间选择器,选择成功后打印出来的 是中国标准时间,需要转成时间戳 后台方便存储
<el-date-picker type="date" v-model="end" placeholder="选择日期" style="width: 100%;" @change="chantime">
data 里要声明一下
end: new Date(),
点击事件是 change 不是click ,用的时候看一下文档写的各种属性。加上下面的就可以了
chantime() {
let self = this;
if (self.end == null) {
return
}
let ends = self.end.getTime();
self.end = ends;
},