1.form表单的使用
标签
<a-form ref="refForm" :form="form" :colon="false" layout="inline" labelAlign="right">
<a-row>
<a-col class="left-colon-padding" :lg="6" :sm="0" />
<a-col :lg="16" :sm="24">
<a-form-item v-else label="手机号码" required>
<a-input v-decorator="['telephone', formRules.mobile]" placeholder="请输入手机号码" />
</a-form-item>
</a-col>
</a-row>
...
js使用
export default {
data () {
return {
form: this.$form.createForm(this), // 表单ref
}
},
methods: {
submit () {
// 刷新表单
this.form.resetFields()
// 设置表单项初始值
this.form.getFieldDecorator('telephone', { initialValue: this.basicData.telephone, preserve: true })
// 获取表单项值
this.form.getFieldValue('telephone')
// 设置表单项值
this.form.setFieldsValue({ 'telephone': 15236958642})
}
}
},
二,图片上传封装组件
<template>
<div>
<a-upload
name="file"
list-type="picture-card"
:class="['avatar-uploader', isCard ? (isFront ? 'front-uploader' : 'back-uploader') : '']"
:show-upload-list="false"
:action="requestUrl"
:headers="headers"
:before-upload="beforeUpload"
:remove="removeFn"
:data="reporterType"
method="post"
@change="handleChange"
:file="requestUrl"
v-decorator="[
'file',
{
getValueFromEvent: normFile,
rules: [{ required: true, message: '请上传证件照' }],
trigger: ['blur']
}
]"
>
<div v-if="imagePreviewUrl" class="upload-img-wrap">
<img :src="imagePreviewUrl" alt="avatar" />
<div>重新上传</div>
</div>
<div v-else>
<a-icon v-if="loading" type="loading" />
<img v-else :src="require('@/assets/add.png')" alt="" style="width: 22px;margin-top:13px" />
<div class="ant-upload-text">
{{ uploadText ? uploadText : '上传图片' }}
</div>
</div>
</a-upload>
<div class="upload-info">仅支持.jpg格式<span v-if="imageUrl" @click="emptyImg">删除</span></div>
</div>
</template>
<script>
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
function getBase64 (img, callback) {
const reader = new FileReader()
reader.addEventListener('load', () => callback(reader.result))
reader.readAsDataURL(img)
}
export default {
props: {
/**
* isFront 是否为身份证正面 false 不是正面
*/
isFront: {
type: Boolean,
default: false
},
isCard: {
type: Boolean,
default: false
},
uploadText: {
type: String,
default: ''
},
imageUrl: {
type: String,
default: ''
},
reporterType: {
type: Object,
default: null
},
changImgStaus: {
type: Boolean,
default: false
}
},
data () {
return {
loading: false,
// imageUrl: this.imageUrl1,
imagePreviewUrl: '',
requestUrl: process.env.VUE_APP_API_BASE_URL + '/file/uploadFile',
headers: {
Authorization: storage.get(ACCESS_TOKEN)
}
}
},
watch: {
imageUrl: {
handler (val) {
this.imagePreviewUrl = val ? process.env.VUE_APP_API_BASE_File_URL + val : ''
},
immediate: true
},
changImgStaus (val) {
if (!val) {
this.imagePreviewUrl = ''
}
}
},
methods: {
handleChange (info) {
if (info.file.status === 'uploading') {
this.loading = true
return
}
if (info.file.status === 'done') {
const { data = {} } = info.file.response
// Get this url from response in real world.
getBase64(info.file.originFileObj, imageUrl => {
this.imagePreviewUrl = process.env.VUE_APP_API_BASE_File_URL + data.filePath.localPath
this.$emit('change', data.filePath)
this.loading = false
})
}
},
beforeUpload (file) {
const isJpgOrPng = file.type === 'image/jpeg'
if (!isJpgOrPng) {
this.$message.error('仅支持.jpg格式')
}
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
this.$message.error('Image must smaller than 2MB!')
}
return isJpgOrPng && isLt2M
},
removeFn (e) {
this.emptyImg()
},
emptyImg () {
this.imagePreviewUrl = ''
this.$emit('change', null)
},
normFile (e) {
if (Array.isArray(e)) {
return e
}
return e && e.fileList
}
}
}
</script>
<style lang="less" scoped>
/deep/.ant-upload-picture-card-wrapper {
width: 14%;
margin-right: 32px;
}
/deep/.avatar-uploader .ant-upload {
width: 106px;
height: 68px;
}
/deep/.avatar-uploader .ant-upload:hover {
.upload-img-wrap > div {
display: block;
}
}
/deep/.avatar-uploader.front-uploader .ant-upload {
background: url('../../assets/frontIdCard.png');
background-size: 100%;
}
/deep/.avatar-uploader.back-uploader .ant-upload {
background: url('../../assets/sideIdCard.png');
background-size: 100%;
}
/deep/.ant-upload-select-picture-card i {
font-size: 32px;
color: #999;
}
/deep/.ant-upload.ant-upload-select-picture-card .ant-upload {
padding: 0;
}
/deep/.ant-upload-select-picture-card .ant-upload-text {
margin-top: 3px;
font-size: 14px;
font-weight: 500;
color: #878c95;
}
/deep/.upload-img-wrap {
width: 106px;
height: 68px;
position: relative;
img {
width: 106px;
height: 68px;
}
& > div {
width: 106px;
height: 68px;
line-height: 68px;
text-align: center;
border-radius: 4px;
background: rgba(0, 0, 0, 0.64);
position: absolute;
z-index: 10;
left: 0;
top: 0;
font-size: 14px;
color: #fff;
display: none;
}
}
.upload-info {
width: 122px;
font-size: 12px;
color: #afb4b9;
margin-top: 4px;
margin-bottom: 12px;
margin-left: -8px;
text-align: center;
span {
padding-left: 8px;
font-size: 12px;
font-weight: 400;
color: #24a1ff;
cursor: pointer;
}
}
</style>
网友评论