首先在main.js引入 import { Form,Field } from ‘vant’;
Vue.use(Form);
Vue.use(Field);
vue文件 template 写入
<van-field name="uploader" label="文件上传">
<template #input>
<van-uploader
v-model="imgList"
multiple
:max-count="1"
:after-read="toChooseImg"
:before-delete="delPic"
/>
</template>
</van-field>
vue文件 js 写入
data() {
return {
files: [], //图片
imgList: [],
};
},
methods: {
// 图片信息
toChooseImg(file) {
let localData = file.content;
this.uploadImg(localData.split(",")[1]);
},
// 上传图片到服务器
uploadImg(img) {
this.$http
.postRequest("/frontEnd/F/F001", {
file_type: "1",
attach_type: "photo",
file_ext: "jpeg",
image: img,
toAliyun: "1",
})
.then((res) => {
if (res.result != "0") {
this.$toast.fail(res.msg);
} else {
this.files.push(res.filePath);
}
});
},
// 删除图片
delPic(idx) {
this.imgList.splice(idx, 1);
this.files.splice(idx - 1, 1);
},
},