Image 图片

基础用法

  1. {
  2. "resources": {
  3. "imageRes": {
  4. "fields": {
  5. "image1": {
  6. "label": "图片上传",
  7. "type": "image",
  8. "rules": [{
  9. "required": true,
  10. "message": "请上传图片",
  11. "trigger": "change"
  12. }]
  13. }
  14. }
  15. }
  16. },
  17. "blocks": {
  18. "editImage": {
  19. "type": "form",
  20. "resource": "imageRes",
  21. "ctx": "edit",
  22. "style": {
  23. "width": "50%"
  24. },
  25. "events": {
  26. "submit": "@validate @create"
  27. },
  28. "operations": {
  29. "submit": {
  30. "type": "button",
  31. "label": "提交"
  32. }
  33. }
  34. },
  35. "viewImage": {
  36. "type": "form",
  37. "resource": "imageRes",
  38. "ctx": "view",
  39. "style": {
  40. "width": "50%"
  41. }
  42. }
  43. }
  44. }

显示配置

大小、类型限制

可以通过 field.check 配置限制条件,支持 maxSizeInKB, imgMaxWidth, imgMaxHeight, imgMinWidth, imgMinHeight, imgWidth, imgHeight

  1. {
  2. "type": "form",
  3. "resource": {
  4. "fields": {
  5. "image3": {
  6. "label": "尺寸范围",
  7. "type": "image",
  8. "tip": "范围240-640",
  9. "successUrlKey": "url",
  10. "check": {
  11. "imgMaxWidth": 640,
  12. "imgMaxHeight": 640,
  13. "imgMinWidth": 240,
  14. "imgMinHeight": 240
  15. },
  16. "props": {
  17. "action": "https://easy-mock.com/mock/5a0023effbbb09615044cb82/upload-image"
  18. }
  19. },
  20. "image4": {
  21. "label": "固定尺寸",
  22. "type": "image",
  23. "tip": "640x640",
  24. "successUrlKey": "url",
  25. "check": {
  26. "imgWidth": 640,
  27. "imgHeight": 640
  28. },
  29. "props": {
  30. "action": "https://easy-mock.com/mock/5a0023effbbb09615044cb82/upload-image"
  31. }
  32. },
  33. "image5": {
  34. "label": "图片类型",
  35. "type": "image",
  36. "tip": "只能上传png文件",
  37. "successUrlKey": "url",
  38. "props": {
  39. "accept": "image/png",
  40. "action": "https://easy-mock.com/mock/5a0023effbbb09615044cb82/upload-image"
  41. }
  42. }
  43. }
  44. },
  45. "ctx": "edit"
  46. }

显示配置

带已上传列表

可以通过 field.props['file-list']field.props['list-type']配置,而list-type 可取值text/picture/picture-card,默认为 text

  1. {
  2. "type": "form",
  3. "resource": {
  4. "fields": {
  5. "image1": {
  6. "label": "默认",
  7. "type": "image",
  8. "tip": "只能不超过50kb的图片",
  9. "props": {
  10. "action": "https://easy-mock.com/mock/5a0023effbbb09615044cb82/upload-image",
  11. "file-list": [{
  12. "name": "food.jpeg",
  13. "url": "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
  14. }, {
  15. "name": "精选美景图片",
  16. "url": "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
  17. }]
  18. }
  19. },
  20. "image2": {
  21. "label": "picture",
  22. "type": "image",
  23. "tip": "只能不超过50kb的图片",
  24. "props": {
  25. "action": "https://easy-mock.com/mock/5a0023effbbb09615044cb82/upload-image",
  26. "list-type": "picture",
  27. "file-list": [{
  28. "name": "food.jpeg",
  29. "url": "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
  30. }, {
  31. "name": "精选美景图片",
  32. "url": "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
  33. }]
  34. }
  35. },
  36. "image3": {
  37. "label": "picture-card",
  38. "type": "image",
  39. "tip": "只能不超过50kb的图片",
  40. "props": {
  41. "action": "https://easy-mock.com/mock/5a0023effbbb09615044cb82/upload-image",
  42. "list-type": "picture-card",
  43. "file-list": [{
  44. "name": "food.jpeg",
  45. "url": "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
  46. }, {
  47. "name": "精选美景图片",
  48. "url": "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
  49. }]
  50. }
  51. }
  52. }
  53. },
  54. "ctx": "edit"
  55. }

显示配置

带默认图列表

可以通过 field.props['default-image-list'] 配置,适用从默认列表里选择一张或者上传的场景。

  1. {
  2. "type": "form",
  3. "resource": {
  4. "fields": {
  5. "image3": {
  6. "label": "默认图列表",
  7. "type": "image",
  8. "props": {
  9. "action": "https://easy-mock.com/mock/5a0023effbbb09615044cb82/upload-image",
  10. "default-image-list": [{
  11. "name": "food.jpeg",
  12. "url": "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
  13. }, {
  14. "name": "精选美景图片精选美景图片",
  15. "url": "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
  16. }]
  17. }
  18. }
  19. }
  20. },
  21. "ctx": "edit"
  22. }

显示配置

在线实验室

Image 图片 - 图1

Image 图片 - 图2

参数列表

参数说明可选值 | 类型必填
typefield类型string
label标签文本string
ctx编辑态为edit,展示态为view,不配置则使用当前block的ctx"edit" | "view"
default可以设置默认值string
labelWidth在form和object内生效,可单独指定当前field的标签文本所占的宽度string
info如配置会在标签处展示tooltip提示string
desc如配置会在ctx为edit状态下固定显示在表单项下面string
hidden默认为false,配置为true则改表单项会隐藏null | boolean
show配置后只有满足指定的条件后才显示,实现对目标条件的依赖联动string | object | function
show.name目标字段名string
show.value目标字段值string | number
rules可以配置多条校验规则,会在form的编辑态通过@validate来校验,详见 https://github.com/yiminghe/async-validatornull | array
rules[]object
rules[].require是否必填boolean
rules[].type校验类型"string" | "number" | "boolean" | "method" | "regexp" | "integer" | "float" | "array" | "object" | "enum" | "date" | "url" | "hex" | "email"
propsaction为上传接口地址null | object
successUrlKey调用action上传接口成功后会通过该路径去获取url值string
tip固定显示的tipstring
check可以配置图片校验参数,先进行本地校验,符合条件的才上传null | object

组件更多配置可参考Element:Upload 上传Image 图片 - 图3中的Attributes