AntDesign vue学习笔记(七)Form 读写与图片上传

AntDesign Form使用布局相比传统Jquery有点繁琐

(一)先读写一个简单的input为例

<a-form :form="form" layout="vertical" hideRequiredMark>
        <a-row>
          <a-col :span="8">
            <a-form-item label="用户名">
              <a-input
                v-decorator="['username', {rules: [{ required: true, message: '用户名' }]}]"
                placeholder
              />
            </a-form-item>
....

1、读数据,很简单

this.form.validateFields((err, values) => {
if (!err) {
this.form.getFieldValue("username");
注:此处也可以直接拿values中值使用
2、写数据,根据经验把get变成set,提示不存在setFieldValue(!-_-)
换一个
this.form.setFieldsValue('username', '测试')
执行一直不成功,提示
browser.js?1af0:49 Warning: You cannot set a form field before rendering a field associated with the value.
网上查各种资料未找到原因,通过以下方法尝试解决
(1)this.form.getFieldDecorator('username', { initialValue: '' })无效果
(2)setTimeout无效果
(3)最终发现需要这样写
this.form.setFieldsValue({
'username': '测试'
})
注意正确应该多一对 {},很奇怪为啥没有setFieldValue
函数原型:setFieldsValue Set the value of a field. Function({ [fieldName]: value }
 
(二)上传图片
1、data()中定义FileList,初始化图片如下面格式(可以不初始化)
      fileList: [{
        uid: '-1',
        name: 'xxx.png',
        status: 'done',
        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
      }]

 2、下面是点击图片后自动上传写法,可以将action替换为你自己的上传图片后台地址

        <a-row>
          <a-col :span="12">
            <a-form-item label="图片">
            <a-upload
              action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
              listType="picture-card"
              :fileList="fileList"
              @preview="handlePreview"
              @change="handleChange"
            >
              <div v-if="fileList.length < 1">
                <a-icon type="plus"/>
                <div class="ant-upload-text">上传图片</div>
              </div>
            </a-upload>
            </a-form-item>
          </a-col>
        </a-row>
    handleCancel () {
      this.previewVisible = false
    },
    handlePreview (file) {
      this.previewImage = file.url || file.thumbUrl
      this.previewVisible = true
    },
    handleChange ({ fileList }) {
      this.fileList = fileList
    }
.ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }
  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }

3、当选择图片时已经自动调用action接口,后台返回数据如下

{
    "name": "xxx.png",
    "status": "done",
    "url": "https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png",
    "thumbUrl": "https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
}

4、到此时已经将图片上传到了服务上了,实际项目中需要同时上传token,就需要使用其他写法,等后续代码实现后将贴出来。

转载于:https://www.cnblogs.com/zhaogaojian/p/11146536.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你的问题是如何在 Ant Design 的 Table 中显示预览图片。对吧? 如果是这样的话,可以考虑使用 Ant Design 中的 Upload 组件和 Modal 组件来实现。具体步骤如下: 1. 在 Table 中,使用 render 函数来渲染每一行的数据,其中包括需要显示预览图片的列。 2. 在 render 函数中,使用 Upload 组件来上传图片,并将图片的 url 存储在 state 中。 3. 在 render 函数中,使用 Modal 组件来显示预览图片。当用户点击图片时,Modal 组件会显示图片的预览。 下面是一个简单的示例代码: ```javascript <template> <a-table :columns="columns" :data-source="data"> <template #avatar="text"> <a-upload :show-upload-list="false" :before-upload="handleUpload" > <a-avatar :src="avatarUrl" /> </a-upload> <a-modal :visible="previewVisible" @cancel="handleCancel"> <a-img :src="previewImage" /> </a-modal> </template> </a-table> </template> <script> export default { data() { return { data: [ { name: "John Brown", age: 32, avatarUrl: "", }, ], previewVisible: false, previewImage: "", }; }, methods: { handleUpload(file) { // 上传图片,并将图片的 url 存储在 state 中 this.avatarUrl = URL.createObjectURL(file.raw); }, handlePreview(url) { // 显示图片预览 this.previewImage = url; this.previewVisible = true; }, handleCancel() { // 隐藏图片预览 this.previewVisible = false; }, }, computed: { columns() { return [ { title: "Name", dataIndex: "name", }, { title: "Age", dataIndex: "age", }, { title: "Avatar", dataIndex: "avatarUrl", scopedSlots: { customRender: "avatar" }, width: "20%", }, ]; }, }, }; </script> ``` 在这个示例中,我们使用了 Upload 组件来上传图片,并将图片的 url 存储在 state 中。在 render 函数中,我们使用了 Modal 组件来显示预览图片。当用户点击图片时,Modal 组件会显示图片的预览。 希望这可以帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值