事件动作

1.7.0 及以上版本

上一节我们介绍了如何实现联动,是比较基础和局限的,而事件动作是更简单、更灵活、更高级的用法,可以解决复杂的 UI 交互场景,支持渲染器事件监听和响应设计,无需关心组件层级关系。例如:

  • 传递数据
    • 点击「按钮」,发送一个远程请求,返回的数据回填给一个表单
    • 弹窗提交后,将弹窗内表单数据回填给另一个表单
  • 联动刷新
    • 下拉选择不同项,联动刷新表单数据
  • 状态控制
    • 勾选不同按钮,控制相应组件的显示/隐藏
    • 勾选不同按钮,控制相应组件的启用/禁用
  • 多个组件监听同一个事件做出不同响应
    • 下拉选择不同项,组件 A 监听后发送一个远程请求,组件 B 监听后进行刷新
  • 逻辑编排
    • 针对监听到的事件,循环执行一些动作作为响应,还可以控制循环跳出或跳过
    • 针对监听到的事件,根据条件选择性的执行动作响应
    • 针对监听到的事件,并行执行多个动作作为响应
    • 执行完当前动作后,可以选择是否继续执行后续动作,是否关闭事件默认行为的执行

基本使用

通过onEvent属性实现渲染器事件与响应动作的绑定。onEvent内配置事件和动作映射关系,actions是事件对应的响应动作的集合。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '尝试点击、鼠标移入/移出',
  7. level: 'primary',
  8. onEvent: {
  9. click: {
  10. actions: [
  11. {
  12. actionType: 'toast',
  13. args: {
  14. msgType: 'info',
  15. msg: '派发点击事件'
  16. }
  17. }
  18. ]
  19. },
  20. mouseenter: {
  21. actions: [
  22. {
  23. actionType: 'toast',
  24. args: {
  25. msgType: 'info',
  26. msg: '派发鼠标移入事件'
  27. }
  28. }
  29. ]
  30. },
  31. mouseleave: {
  32. actions: [
  33. {
  34. actionType: 'toast',
  35. args: {
  36. msgType: 'info',
  37. msg: '派发鼠标移出事件'
  38. }
  39. }
  40. ]
  41. }
  42. }
  43. }
  44. ]
  45. }

事件与动作

事件包含渲染器事件广播事件,监听这些事件时,可以通过event.data来获取事件对象的数据。

  • 渲染器事件,由具体的渲染器组件提供,每个渲染器组件暴露的事件可以查看具体的组件文档的事件表
  • 广播事件,即自定义事件,可以自定义派发的事件名称eventName,其他渲染器可以监听该自定义事件并配置响应动作。

动作包含通用动作组件动作广播动作自定义动作,可以通过配置actionType来指定具体执行什么动作。

触发通用动作

通用动作包含发送 http 请求、跳转链接、浏览器回退、浏览器刷新、打开/关闭弹窗、打开/关闭抽屉、打开对话框、弹出 Toast 提示、复制、发送邮件、刷新、控制显示隐藏、控制启用禁用状态、更新数据。

发送 http 请求

通过配置actionType: 'ajax'api实现 http 请求发送,该动作需实现 env.fetcher(config: fetcherConfig) => Promise<fetcherResult>。

  1. {
  2. type: 'page',
  3. data: {
  4. name: 'lll'
  5. },
  6. body: [
  7. {
  8. type: 'button',
  9. id: 'b_001',
  10. label: '发送 Ajax 请求',
  11. level: 'primary',
  12. "confirmText": "确认要发出这个请求?",
  13. onEvent: {
  14. click: {
  15. actions: [
  16. {
  17. actionType: 'ajax',
  18. args: {
  19. api: {
  20. url: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm?name=${name}',
  21. method: 'get'
  22. },
  23. messages: {
  24. success: '成功了!欧耶',
  25. failed: '失败了呢。。'
  26. },
  27. age: 18
  28. }
  29. }
  30. ]
  31. }
  32. }
  33. }
  34. ]
  35. }

动作属性

属性名 类型 默认值 说明
actionType string ajax ajax 请求
args.api API - 接口配置,< 1.8.0 及以下版本 为 api
args.options object - 其他配置,< 1.8.0 及以下版本 为 options
args.messages {success: string, failed: string} - 请求成功/失败后的提示信息,< 1.8.0 及以下版本 为 messages

打开弹窗(模态)

通过配置actionType: 'dialog'dialog实现 Dialog 弹窗。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. className: 'ml-2',
  7. label: '打开弹窗(模态)',
  8. level: 'primary',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'dialog',
  14. dialog: {
  15. type: 'dialog',
  16. title: '模态弹窗',
  17. id: 'dialog_001',
  18. data: {
  19. myage: '22'
  20. },
  21. body: [
  22. {
  23. type: 'tpl',
  24. tpl: '<p>对,你打开了模态弹窗</p>',
  25. inline: false
  26. },
  27. {
  28. type: 'input-text',
  29. name: 'myname',
  30. mode: 'horizontal',
  31. onEvent: {
  32. change: {
  33. actions: [
  34. {
  35. actionType: 'confirm',
  36. componentId: 'dialog_001'
  37. }
  38. ]
  39. }
  40. }
  41. }
  42. ],
  43. onEvent: {
  44. confirm: {
  45. actions: [
  46. {
  47. actionType: 'toast',
  48. args: {
  49. msg: 'confirm'
  50. }
  51. }
  52. ]
  53. },
  54. cancel: {
  55. actions: [
  56. {
  57. actionType: 'toast',
  58. args: {
  59. msg: 'cancel'
  60. }
  61. }
  62. ]
  63. }
  64. }
  65. }
  66. }
  67. ]
  68. }
  69. }
  70. }
  71. ]
  72. }

动作属性

属性名 类型 默认值 说明
actionType string dialog 点击后显示一个弹出框
dialog string/DialogObject - 指定弹框内容,格式可参考Dialog

关闭弹窗(模态)

通过配置actionType: 'closeDialog'实现关闭当前弹窗;附加配置componentId可以实现关闭指定弹窗。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. id: 'b_003',
  7. label: '打开弹窗(模态)',
  8. level: 'primary',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'dialog',
  14. dialog: {
  15. type: 'dialog',
  16. id: 'dialog_002',
  17. title: '模态弹窗',
  18. body: [
  19. {
  20. type: 'button',
  21. label: '打开子弹窗,然后关闭它的父亲',
  22. onEvent: {
  23. click: {
  24. actions: [
  25. {
  26. actionType: 'dialog',
  27. dialog: {
  28. type: 'dialog',
  29. title: '模态子弹窗',
  30. body: [
  31. {
  32. type: 'button',
  33. label: '关闭指定弹窗(关闭父弹窗)',
  34. onEvent: {
  35. click: {
  36. actions: [
  37. {
  38. actionType: 'closeDialog',
  39. componentId: 'dialog_002'
  40. }
  41. ]
  42. }
  43. }
  44. }
  45. ]
  46. }
  47. }
  48. ]
  49. }
  50. }
  51. },
  52. {
  53. type: 'button',
  54. label: '关闭当前弹窗',
  55. className: 'ml-2',
  56. onEvent: {
  57. click: {
  58. actions: [
  59. {
  60. actionType: 'closeDialog'
  61. }
  62. ]
  63. }
  64. }
  65. }
  66. ]
  67. }
  68. }
  69. ]
  70. }
  71. }
  72. }
  73. ]
  74. }

动作属性

属性名 类型 默认值 说明
actionType string closeDialog 关闭弹窗
componentId string - 指定弹框组件 id

打开抽屉(模态)

通过配置actionType: 'drawer'drawer实现 Drawer 抽屉打开。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. id: 'b_004',
  7. level: 'primary',
  8. label: '打开抽屉(模态)',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'drawer',
  14. drawer: {
  15. type: 'drawer',
  16. title: '模态抽屉',
  17. body: [
  18. {
  19. type: 'tpl',
  20. tpl: '<p>对,你打开了模态抽屉</p>',
  21. inline: false
  22. }
  23. ],
  24. onEvent: {
  25. confirm: {
  26. actions: [
  27. {
  28. actionType: 'toast',
  29. args: {
  30. msg: 'confirm'
  31. }
  32. }
  33. ]
  34. },
  35. cancel: {
  36. actions: [
  37. {
  38. actionType: 'toast',
  39. args: {
  40. msg: 'cancel'
  41. }
  42. }
  43. ]
  44. }
  45. }
  46. }
  47. }
  48. ]
  49. }
  50. }
  51. }
  52. ]
  53. }

动作属性

属性名 类型 默认值 说明
actionType string drawer 点击后显示一个侧边栏
drawer string/DrawerObject - 指定弹框内容,格式可参考Drawer

关闭抽屉(模态)

通过配置actionType: 'closeDrawer'实现关闭当前抽屉;附加配置componentId可以实现关闭指定抽屉。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '打开抽屉(模态)',
  7. level: 'primary',
  8. onEvent: {
  9. click: {
  10. actions: [
  11. {
  12. actionType: 'drawer',
  13. drawer: {
  14. type: 'drawer',
  15. id: 'drawer_1',
  16. title: '模态抽屉',
  17. body: [
  18. {
  19. type: 'button',
  20. label: '打开子抽屉,然后关闭它的父亲',
  21. onEvent: {
  22. click: {
  23. actions: [
  24. {
  25. actionType: 'drawer',
  26. drawer: {
  27. type: 'drawer',
  28. title: '模态子抽屉',
  29. body: [
  30. {
  31. type: 'button',
  32. label: '关闭指定抽屉(关闭父抽屉)',
  33. onEvent: {
  34. click: {
  35. actions: [
  36. {
  37. actionType: 'closeDrawer',
  38. componentId: 'drawer_1'
  39. }
  40. ]
  41. }
  42. }
  43. }
  44. ]
  45. }
  46. }
  47. ]
  48. }
  49. }
  50. },
  51. {
  52. type: 'button',
  53. label: '关闭当前抽屉',
  54. className: 'ml-2',
  55. onEvent: {
  56. click: {
  57. actions: [
  58. {
  59. actionType: 'closeDrawer'
  60. }
  61. ]
  62. }
  63. }
  64. }
  65. ]
  66. }
  67. }
  68. ]
  69. }
  70. }
  71. }
  72. ]
  73. }

动作属性

属性名 类型 默认值 说明
actionType string closeDrawer 关闭抽屉
componentId string - 指定抽屉组件 id

打开对话框

通过配置actionType: 'alert'actionType: 'confirm'打开不同对话框,该动作分别需实现 env.alert: (msg: string) => void 和 env.confirm: (msg: string, title?: string) => boolean | Promise<boolean>。

提示对话框

  1. {
  2. type: 'page',
  3. data: {
  4. msg: '去吃饭了'
  5. },
  6. body: [
  7. {
  8. type: 'button',
  9. label: '提示对话框(模态)',
  10. level: 'primary',
  11. onEvent: {
  12. click: {
  13. actions: [
  14. {
  15. actionType: 'alert',
  16. args: {
  17. msg: '<a href="http://www.baidu.com" target="_blank">${msg}~</a>'
  18. }
  19. }
  20. ]
  21. }
  22. }
  23. }
  24. ]
  25. }

动作属性

属性名 类型 默认值 说明
actionType string alert 打开提示对话框
args.msg string - 对话框提示内容,< 1.8.0 及以下版本 为 msg

确认对话框

  1. {
  2. type: 'page',
  3. data: {
  4. title: '操作确认',
  5. msg: '要删除它吗?'
  6. },
  7. body: [
  8. {
  9. type: 'button',
  10. label: '确认对话框(模态)',
  11. level: 'primary',
  12. onEvent: {
  13. click: {
  14. actions: [
  15. {
  16. actionType: 'confirmDialog',
  17. args: {
  18. title: '${title}',
  19. msg: '<span style="color:red">${msg}</span>'
  20. }
  21. }
  22. ]
  23. }
  24. }
  25. }
  26. ]
  27. }

动作属性

属性名 类型 默认值 说明
actionType string confirmDialog 打开确认对话框
args.title string - 对话框标题,< 1.8.0 及以下版本 为 title
args.msg string - 对话框提示内容,< 1.8.0 及以下版本 为 msg

跳转链接

通过配置actionType: 'url'actionType: 'link'实现链接跳转,该动作需实现 env.jumpTo(to: string, action?: any) => void 方法。

打开页面链接

  1. {
  2. type: 'page',
  3. data: {
  4. myname: 'lvxj',
  5. myjon: 'player'
  6. },
  7. body: [
  8. {
  9. type: 'button',
  10. label: '百度一下',
  11. level: 'primary',
  12. className: 'ml-2',
  13. onEvent: {
  14. click: {
  15. actions: [
  16. {
  17. actionType: 'url',
  18. args: {
  19. url: 'http://www.baidu.com',
  20. blank: true,
  21. params: {
  22. name: 'jack',
  23. jon: '${myjon}'
  24. },
  25. name: '${myname}',
  26. age: 18
  27. }
  28. }
  29. ]
  30. }
  31. }
  32. }
  33. ]
  34. }

动作属性

属性名 类型 默认值 说明
actionType string url 页面跳转
args.url string - 按钮点击后,会打开指定页面。可用 ${xxx} 取值,< 1.8.0 及以下版本 为 url
args.blank boolean false 如果为 true 将在新 tab 页面打开,< 1.8.0 及以下版本 为 blank
args.params object - 页面参数{key:value},支持数据映射,> 1.10.0 及以上版本

打开单页链接

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '跳转至「表达式」',
  7. level: 'primary',
  8. className: 'ml-2',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'link',
  14. args: {
  15. link: './expression'
  16. }
  17. }
  18. ]
  19. }
  20. }
  21. }
  22. ]
  23. }

动作属性 | 属性名 | 类型 | 默认值 | 说明 | | ————— | ———— | ——— | —————————————————————————————————————————————————————————- | | actionType | string | link | 单页跳转 | | args.link | string | link | 用来指定跳转地址,跟 url 不同的是,这是单页跳转方式,不会渲染浏览器,请指定 amis 平台内的页面。可用 ${xxx} 取值,< 1.8.0 及以下版本 为 link | | args.params | object | - | 页面参数{key:value},支持数据映射,> 1.9.0 及以上版本 |

浏览器回退

1.8.0 及以上版本

通过配置actionType: 'goBack'实现页面回退。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '回退',
  7. level: 'primary',
  8. className: 'ml-2',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'goBack'
  14. }
  15. ]
  16. }
  17. }
  18. }
  19. ]
  20. }

动作属性

属性名 类型 默认值 说明
actionType string goBack 返回上个页面

前进/后退到指定页面

1.8.0 及以上版本

通过配置actionType: 'goPage'实现浏览器页面的前进/后退。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '后退到上上个页面',
  7. level: 'primary',
  8. className: 'ml-2',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'goPage',
  14. delta: -2
  15. }
  16. ]
  17. }
  18. }
  19. }
  20. ]
  21. }

动作属性

属性名 类型 默认值 说明
actionType string goPage 前进/后退到页面
args.delta string 0 位置,< 1.8.0 及以下版本 为 delta

浏览器刷新

1.8.0 及以上版本

通过配置actionType: 'refresh'实现浏览器刷新。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '刷新页面',
  7. level: 'primary',
  8. className: 'ml-2',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'refresh'
  14. }
  15. ]
  16. }
  17. }
  18. }
  19. ]
  20. }

动作属性

属性名 类型 默认值 说明
actionType string refresh 返回上个页面

toast 提示

通过配置actionType: 'toast'msg实现弹出 toast 提示,该动作需实现 env.notify(type: ToastLevel, msg: string, conf?: ToastConf) => void 方法。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '警告',
  7. level: 'warning',
  8. className: 'mr-2 mb-2',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'toast',
  14. args: {
  15. msgType: 'warning',
  16. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  17. position: 'top-right'
  18. }
  19. }
  20. ]
  21. }
  22. }
  23. },
  24. {
  25. type: 'button',
  26. label: '成功',
  27. level: 'success',
  28. className: 'mr-2 mb-2',
  29. onEvent: {
  30. click: {
  31. actions: [
  32. {
  33. actionType: 'toast',
  34. args: {
  35. msgType: 'success',
  36. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  37. position: 'top-right'
  38. }
  39. }
  40. ]
  41. }
  42. }
  43. },
  44. {
  45. type: 'button',
  46. label: '错误',
  47. level: 'danger',
  48. className: 'mr-2 mb-2',
  49. onEvent: {
  50. click: {
  51. actions: [
  52. {
  53. actionType: 'toast',
  54. args: {
  55. msgType: 'error',
  56. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  57. position: 'top-right'
  58. }
  59. }
  60. ]
  61. }
  62. }
  63. },
  64. {
  65. type: 'button',
  66. label: '提示',
  67. level: 'info',
  68. className: 'mb-2',
  69. onEvent: {
  70. click: {
  71. actions: [
  72. {
  73. actionType: 'toast',
  74. args: {
  75. msgType: 'info',
  76. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  77. position: 'top-right'
  78. }
  79. }
  80. ]
  81. }
  82. }
  83. },
  84. {
  85. type: 'tpl',
  86. tpl: '<br>',
  87. },
  88. {
  89. type: 'button',
  90. label: '左上',
  91. className: 'mr-2 mt-2',
  92. onEvent: {
  93. click: {
  94. actions: [
  95. {
  96. actionType: 'toast',
  97. args: {
  98. msgType: 'info',
  99. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  100. position: 'top-left'
  101. }
  102. }
  103. ]
  104. }
  105. }
  106. },
  107. {
  108. type: 'button',
  109. label: '中上',
  110. className: 'mr-2 mt-2',
  111. onEvent: {
  112. click: {
  113. actions: [
  114. {
  115. actionType: 'toast',
  116. args: {
  117. msgType: 'info',
  118. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  119. position: 'top-center'
  120. }
  121. }
  122. ]
  123. }
  124. }
  125. },
  126. {
  127. type: 'button',
  128. label: '右上',
  129. className: 'mt-2',
  130. onEvent: {
  131. click: {
  132. actions: [
  133. {
  134. actionType: 'toast',
  135. args: {
  136. msgType: 'info',
  137. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  138. position: 'top-right'
  139. }
  140. }
  141. ]
  142. }
  143. }
  144. },
  145. {
  146. type: 'tpl',
  147. tpl: '<br>',
  148. },
  149. {
  150. type: 'button',
  151. label: '屏幕的中央',
  152. className: 'ml-10 mt-2 mb-2',
  153. onEvent: {
  154. click: {
  155. actions: [
  156. {
  157. actionType: 'toast',
  158. args: {
  159. msgType: 'info',
  160. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  161. position: 'center'
  162. }
  163. }
  164. ]
  165. }
  166. }
  167. },
  168. {
  169. type: 'tpl',
  170. tpl: '<br>',
  171. },
  172. {
  173. type: 'button',
  174. label: '左下',
  175. className: 'mr-2',
  176. onEvent: {
  177. click: {
  178. actions: [
  179. {
  180. actionType: 'toast',
  181. args: {
  182. msgType: 'info',
  183. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  184. position: 'bottom-left'
  185. }
  186. }
  187. ]
  188. }
  189. }
  190. },
  191. {
  192. type: 'button',
  193. label: '中下',
  194. className: 'mr-2',
  195. onEvent: {
  196. click: {
  197. actions: [
  198. {
  199. actionType: 'toast',
  200. args: {
  201. msgType: 'info',
  202. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  203. position: 'bottom-center'
  204. }
  205. }
  206. ]
  207. }
  208. }
  209. },
  210. {
  211. type: 'button',
  212. label: '右下',
  213. onEvent: {
  214. click: {
  215. actions: [
  216. {
  217. actionType: 'toast',
  218. args: {
  219. msgType: 'info',
  220. msg: '我是全局警告消息,可以配置不同类型和弹出位置~',
  221. position: 'bottom-right'
  222. }
  223. }
  224. ]
  225. }
  226. }
  227. }
  228. ]
  229. }

动作属性

属性名 类型 默认值 说明
actionType string "toast" 指定 toast 动作
args.msgType string "info" 消息类型 `info\ success\ error\ warning< 1.8.0 及以下版本 为 msgType`
args.msg string - 消息内容,< 1.8.0 及以下版本 为 msg
args.position string top-center(移动端为center) 提示显示位置 `top-right\ top-center\ top-left\ bottom-center\ bottom-left\ bottom-right\ center< 1.8.0 及以下版本 为 position`
args.closeButton boolean false 是否展示关闭按钮,< 1.8.0 及以下版本 为 closeButton
args.showIcon boolean true 是否展示图标,< 1.8.0 及以下版本 为 showIcon
args.timeout number 5000(error类型为6000,移动端为3000) 持续时间,< 1.8.0 及以下版本 为 timeout

复制

通过配置actionType: 'copy'和复制属性实现文本的复制操作,该动作需实现 env.copy(contents: string, options?: any) => void 方法。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '复制一段文本',
  7. level: 'primary',
  8. className: 'mr-2',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'copy',
  14. args: {
  15. content: 'http://www.baidu.com'
  16. }
  17. }
  18. ]
  19. }
  20. }
  21. },
  22. {
  23. type: 'button',
  24. label: '复制一段富文本',
  25. level: 'primary',
  26. onEvent: {
  27. click: {
  28. actions: [
  29. {
  30. actionType: 'copy',
  31. args: {
  32. copyFormat: 'text/html',
  33. content: "<a href='http://www.baidu.com'>link</a> <b>bold</b>"
  34. }
  35. }
  36. ]
  37. }
  38. }
  39. }
  40. ]
  41. }

动作属性

属性名 类型 默认值 说明
actionType string copy 复制一段内容到粘贴板
args.copyFormat string text/html 复制格式,< 1.8.0 及以下版本 为 copyFormat
args.content 模板 - 指定复制的内容。可用 ${xxx} 取值,< 1.8.0 及以下版本 为 content

发送邮件

通过配置actionType: 'email'和邮件属性实现发送邮件操作。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '发送邮件',
  7. level: 'primary',
  8. onEvent: {
  9. click: {
  10. actions: [
  11. {
  12. actionType: 'email',
  13. args: {
  14. to: 'amis@baidu.com',
  15. cc: 'baidu@baidu.com',
  16. subject: '这是邮件主题',
  17. body: '这是邮件正文'
  18. }
  19. }
  20. ]
  21. }
  22. }
  23. }
  24. ]
  25. }

动作属性

属性名 类型 默认值 说明
actionType string email 点击后显示一个弹出框
args.to string - 收件人邮箱,可用 ${xxx} 取值,< 1.8.0 及以下版本 为 to
args.cc string - 抄送邮箱,可用 ${xxx} 取值,< 1.8.0 及以下版本 为 cc
args.bcc string - 匿名抄送邮箱,可用 ${xxx} 取值,< 1.8.0 及以下版本 为 bcc
args.subject string - 邮件主题,可用 ${xxx} 取值,< 1.8.0 及以下版本 为 subject
args.body string - 邮件正文,可用 ${xxx} 取值,< 1.8.0 及以下版本 为 body

刷新

通过配置actionType: 'reload'实现对指定组件的刷新(重新加载)操作,仅支持formwizardservicepageappchartcrud,以及支持动态数据的输入类组件,详见组件的动作表。更多示例请查看刷新示例

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '刷新',
  7. className: 'mb-2',
  8. level: 'primary',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'reload',
  14. componentId: 'form-reload'
  15. }
  16. ]
  17. }
  18. }
  19. },
  20. {
  21. type: 'form',
  22. id: 'form-reload',
  23. name: 'form-reload',
  24. initApi:
  25. 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/page/initData',
  26. title: '表单',
  27. body: [
  28. {
  29. type: 'input-text',
  30. id: 'date-input-01',
  31. name: 'date',
  32. label: '时间戳'
  33. }
  34. ]
  35. }
  36. ]
  37. }

动作属性

属性名 类型 默认值 说明
actionType string reload 刷新组件
componentId string - 指定刷新的目标组件 id

显示与隐藏

1.8.0 及以上版本

通过配置actionType: 'show''hidden'实现对指定组件的显示和隐藏,且显隐动作的控制高于组件显隐属性的设置。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '显示',
  7. level: 'primary',
  8. className: 'mr-2',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'show',
  14. componentId: 'input-text_001'
  15. }
  16. ]
  17. }
  18. }
  19. },
  20. {
  21. type: 'button',
  22. label: '隐藏',
  23. level: 'primary',
  24. onEvent: {
  25. click: {
  26. actions: [
  27. {
  28. actionType: 'hidden',
  29. componentId: 'input-text_001'
  30. }
  31. ]
  32. }
  33. }
  34. },
  35. {
  36. type: 'input-text',
  37. label: '愿望',
  38. className: 'mt-2',
  39. id: 'input-text_001',
  40. mode: 'horizontal',
  41. hidden: true
  42. }
  43. ]
  44. }

动作属性

属性名 类型 默认值 说明
actionType string show or hidden 显示或隐藏组件
componentId string - 指定显示或隐藏的目标组件 id

控制状态

1.8.0 及以上版本

通过配置actionType: 'enabled'actionType: 'disabled'实现对指定组件的启用和禁用,仅支持实现了状态控制功能的数据输入类组件。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. id: 'b_dis',
  7. label: '禁用',
  8. level: 'primary',
  9. className: 'mr-2 mb-2',
  10. disabled: true,
  11. onEvent: {
  12. click: {
  13. actions: [
  14. {
  15. actionType: 'disabled',
  16. componentId: 'form_disable'
  17. }
  18. ]
  19. }
  20. }
  21. },
  22. {
  23. type: 'button',
  24. label: '启用',
  25. level: 'primary',
  26. className: 'mb-2',
  27. onEvent: {
  28. click: {
  29. actions: [
  30. {
  31. actionType: 'enabled',
  32. componentId: 'form_disable'
  33. }
  34. ]
  35. }
  36. }
  37. },
  38. {
  39. type: 'form',
  40. id: 'form_disable',
  41. title: '表单',
  42. body: [
  43. {
  44. type: 'group',
  45. body: [
  46. {
  47. type: 'button',
  48. className: 'ml-2',
  49. label: '我的状态变了'
  50. },
  51. {
  52. type: 'button',
  53. className: 'ml-2',
  54. label: '禁用上面的按钮',
  55. level: 'primary',
  56. onEvent: {
  57. click: {
  58. actions: [
  59. {
  60. actionType: 'disabled',
  61. componentId: 'b_dis'
  62. }
  63. ]
  64. }
  65. }
  66. },
  67. {
  68. type: 'button',
  69. className: 'ml-2',
  70. label: '启用用上面的按钮',
  71. level: 'primary',
  72. onEvent: {
  73. click: {
  74. actions: [
  75. {
  76. actionType: 'enabled',
  77. componentId: 'b_dis'
  78. }
  79. ]
  80. }
  81. }
  82. }
  83. ]
  84. }
  85. ]
  86. }
  87. ]
  88. }

动作属性

属性名 类型 默认值 说明
actionType string enabled or disabled 启用或禁用组件
componentId string - 指定启用或禁用的目标组件 id

更新数据

1.8.0 及以上版本

更新数据即变量赋值,通过配置actionType: 'setValue'实现组件数据域变量更新,通过它可以实现组件间联动更新数据回填,支持基础类型对象类型数组类型,数据类型取决于目标组件所需数据值类型,仅支持formdialogdrawerwizardservicepageappchart,以及数据输入类组件。更多示例请查看更新数据示例

  1. {
  2. type: 'page',
  3. title: '更新表单数据',
  4. data: {
  5. globalData: {
  6. myrole: '法官',
  7. mymsg: '该吃饭了!'
  8. }
  9. },
  10. body: [
  11. {
  12. type: 'button',
  13. label: '更新',
  14. level: 'primary',
  15. className: 'mb-2',
  16. onEvent: {
  17. click: {
  18. actions: [
  19. {
  20. actionType: 'setValue',
  21. componentId: 'form_data',
  22. args: {
  23. value: '${globalData}'
  24. }
  25. }
  26. ]
  27. }
  28. }
  29. },
  30. {
  31. type: 'form',
  32. id: 'form_data',
  33. title: '表单',
  34. debug: true,
  35. data: {
  36. myrole: '预言家',
  37. age: '18'
  38. },
  39. "initApi": "/api/mock2/form/initData",
  40. body: [
  41. {
  42. type: 'input-text',
  43. label: '角色',
  44. name: 'myrole',
  45. disabled: false,
  46. mode: 'horizontal'
  47. },
  48. {
  49. type: 'input-text',
  50. label: '年龄',
  51. name: 'age',
  52. disabled: false,
  53. mode: 'horizontal'
  54. }
  55. ]
  56. }
  57. ]
  58. }

动作属性

属性名 类型 默认值 说明
actionType string setValue 变量赋值,即设置组件的数据值
componentId string - 指定赋值的目标组件 id
args.value any - 值,< 1.8.0 及以下版本 为 value

自定义 JS

通过配置actionType: 'custom'实现自定义 JS。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '发送一个 http 请求',
  7. level: 'primary',
  8. onEvent: {
  9. click: {
  10. actions: [
  11. {
  12. actionType: 'custom',
  13. script:
  14. "doAction({actionType: 'ajax', args: {api: '/api/mock2/form/saveForm'}});\n //event.stopPropagation();"
  15. }
  16. ]
  17. }
  18. }
  19. }
  20. ]
  21. }

如果是在 js 中也能直接写函数,这个函数可以接收到 3 个参数,分别是:

  • context,上下文信息
  • doAction 方法,用于调用其它动作
  • event,事件传递的数据,以及可以禁止

动作属性

属性名 类型 默认值 说明
actionType string custom 自定义 JS
script string/function - 自定义 JS 脚本代码,代码内可以通过调用doAction执行任何动作 ,通过事件对象event可以实现事件动作干预

触发其他组件的动作

通过配置componentId来触发指定组件的动作,组件动作配置通过args传入(> 1.9.0 及以上版本),动作参数请查看对应的组件的动作表,更多示例请查看组件事件动作示例

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '选中选项卡2',
  7. level: 'primary',
  8. className: 'mr-3 mb-3',
  9. onEvent: {
  10. click: {
  11. actions: [
  12. {
  13. actionType: 'changeActiveKey',
  14. componentId: 'tabs-change-receiver',
  15. args: {
  16. activeKey: 2
  17. }
  18. }
  19. ]
  20. }
  21. }
  22. },
  23. {
  24. id: 'tabs-change-receiver',
  25. type: 'tabs',
  26. mode: 'line',
  27. tabs: [
  28. {
  29. title: '选项卡1',
  30. body: '选项卡内容1'
  31. },
  32. {
  33. title: '选项卡2',
  34. body: '选项卡内容2'
  35. },
  36. {
  37. title: '选项卡3',
  38. body: '选项卡内容3'
  39. }
  40. ]
  41. }
  42. ]
  43. }

触发广播动作

通过配置actionType: 'broadcast'eventName实现触发一个广播,可以通过配置动作执行优先级weight来控制所有监听者的动作执行顺序。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. "name": "role",
  6. "type": "select",
  7. "label": "广播一下",
  8. "mode": "row",
  9. "options": [
  10. {
  11. "label": "海贼王的男人",
  12. "value": "路飞"
  13. },
  14. {
  15. "label": "海上华佗",
  16. "value": "乔巴"
  17. },
  18. {
  19. "label": "海上食神",
  20. "value": "山治"
  21. }
  22. ],
  23. "onEvent": {
  24. "change": {
  25. "actions": [
  26. {
  27. actionType: 'broadcast',
  28. eventName: 'broadcast_1',
  29. args: {
  30. myrole: '${role}',
  31. age: 18
  32. }
  33. }
  34. ]
  35. }
  36. }
  37. },
  38. {
  39. type: 'form',
  40. id: 'form_001_form_01',
  41. title: '表单1(优先级低)',
  42. name: 'sub-form1',
  43. body: [
  44. {
  45. type: 'input-text',
  46. label: '昵称',
  47. name: 'myname',
  48. disabled: false,
  49. mode: 'horizontal'
  50. }
  51. ],
  52. onEvent: {
  53. broadcast_1: {
  54. actions: [
  55. {
  56. actionType: 'reload',
  57. args: {
  58. myname: '${event.data.value}', // 从事件数据中取
  59. }
  60. },
  61. {
  62. "actionType": "toast",
  63. args: {
  64. "msgType": "info",
  65. "msg": "表单1刷新完成"
  66. }
  67. }
  68. ]
  69. }
  70. }
  71. },
  72. {
  73. type: 'form',
  74. name: 'form2',
  75. id: 'form_002',
  76. title: '表单2(优先级中)',
  77. body: [
  78. {
  79. type: 'input-text',
  80. label: '角色',
  81. name: 'myrole',
  82. disabled: false,
  83. mode: 'horizontal'
  84. },
  85. {
  86. type: 'input-text',
  87. label: '年龄',
  88. name: 'age',
  89. disabled: false,
  90. mode: 'horizontal'
  91. }
  92. ],
  93. onEvent: {
  94. broadcast_1: {
  95. weight: 2,
  96. actions: [
  97. {
  98. actionType: 'reload',
  99. args: {
  100. myrole: '${event.data.value}',
  101. age: '${event.data.age}'
  102. }
  103. },
  104. {
  105. "actionType": "toast",
  106. args: {
  107. "msgType": "info",
  108. "msg": "表单2刷新完成"
  109. }
  110. }
  111. ]
  112. }
  113. }
  114. },
  115. {
  116. type: 'form',
  117. name: 'form3',
  118. id: 'form_003',
  119. title: '表单3(优先级高)',
  120. body: [
  121. {
  122. type: 'input-text',
  123. id: 'form_003_text_01',
  124. label: '职业',
  125. name: 'job',
  126. disabled: false,
  127. mode: 'horizontal'
  128. }
  129. ],
  130. api: 'https://api/form/form3',
  131. onEvent: {
  132. broadcast_1: {
  133. weight: 3,
  134. actions: [
  135. {
  136. actionType: 'reload',
  137. args: {
  138. job: '${event.data.value}'
  139. }
  140. },
  141. {
  142. "actionType": "toast",
  143. args: {
  144. "msgType": "info",
  145. "msg": "表单3刷新完成"
  146. }
  147. }
  148. ]
  149. }
  150. }
  151. }
  152. ]
  153. }

动作属性

属性名 类型 默认值 说明
actionType string broadcast 广播动作
eventName string - 广播动作对应的自定义事件名称,用于广播事件的监听

自定义动作

除了以上内置动作,你还可以注册自己的动作。通过对RendererActionrun方法的实现可以定制自己的动作逻辑,最后通过registerAction注册到 amis 事件动作中。

  1. import {
  2. ListenerAction,
  3. ListenerContext,
  4. registerAction,
  5. RendererAction
  6. } from 'amis-core';
  7. import {RendererEvent} from 'amis-core';
  8. // 动作定义
  9. interface IMyAction extends ListenerAction {
  10. actionType: 'my-action';
  11. args: {
  12. param1: string, // 动作参数1
  13. param2: string // 动作参数2
  14. };
  15. }
  16. /**
  17. * 我的动作实现
  18. */
  19. export class MyAction implements RendererAction {
  20. run(action: IMyAction, renderer: ListenerContext, event: RendererEvent<any>) {
  21. const props = renderer.props;
  22. const {param1, param2} = action.args;
  23. // 你的动作逻辑
  24. // ...
  25. }
  26. }
  27. // 注册自定义动作
  28. registerAction('my-action', new MyAction());

编排动作

通过配置actionType: 'for'actionType: 'break'actionType: 'continue'actionType: 'switch'actionType: 'parallel'实现动作的逻辑编排,支持嵌套。

条件

  1. {
  2. type: 'page',
  3. body: {
  4. type: 'form',
  5. wrapWithPanel: false,
  6. data: {
  7. expression: 'okk'
  8. },
  9. body: [
  10. {
  11. type: 'button',
  12. label: '符合条件的执行',
  13. level: 'primary',
  14. onEvent: {
  15. click: {
  16. actions: [
  17. {
  18. actionType: 'toast',
  19. args: {
  20. msgType: 'success',
  21. msg: '我okk~'
  22. },
  23. expression: 'expression === "okk"'
  24. },
  25. {
  26. actionType: 'toast',
  27. args: {
  28. msg: '1'
  29. },
  30. expression: 'expression === "nono"'
  31. },
  32. {
  33. actionType: 'toast',
  34. args: {
  35. msgType: 'success',
  36. msg: '我也okk~'
  37. },
  38. expression: 'expression === "okk"'
  39. }
  40. ]
  41. }
  42. }
  43. }
  44. ]
  45. }
  46. }

动作属性

属性名 类型 默认值 说明
actionType string for 循环执行动作
expression boolean\ 表达式 - 执行条件,不设置表示默认执行

循环

单层循环

  1. {
  2. type: 'page',
  3. body: {
  4. type: 'form',
  5. wrapWithPanel: false,
  6. data: {
  7. loopName: 'loopData',
  8. loopData: [
  9. {
  10. name: 'lv',
  11. age: '19'
  12. },
  13. {
  14. name: 'xj',
  15. age: '21'
  16. }
  17. ]
  18. },
  19. body: [
  20. {
  21. type: 'button',
  22. label: '循环发送两次请求,且每次携带了循环的数据',
  23. level: 'primary',
  24. onEvent: {
  25. click: {
  26. actions: [
  27. {
  28. actionType: 'loop',
  29. args: {
  30. level: 3,
  31. loopName: '${loopName}'
  32. },
  33. children: [
  34. {
  35. actionType: 'ajax',
  36. args: {
  37. api: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm?name=${name}&age=${age}'
  38. }
  39. }
  40. ]
  41. }
  42. ]
  43. }
  44. }
  45. }
  46. ]
  47. }
  48. }

嵌套循环

  1. {
  2. type: 'page',
  3. body: {
  4. type: 'form',
  5. wrapWithPanel: false,
  6. data: {
  7. loopName: 'loopData',
  8. loopData: [
  9. {
  10. name: 'lv',
  11. age: '19'
  12. },
  13. {
  14. name: 'xj',
  15. age: '21'
  16. }
  17. ]
  18. },
  19. body: [
  20. {
  21. type: 'button',
  22. label: '循环',
  23. className: 'm-2',
  24. onEvent: {
  25. click: {
  26. actions: [
  27. {
  28. actionType: 'loop',
  29. preventDefault: false,
  30. stopPropagation: false,
  31. args: {
  32. loopName: '${loopName}',
  33. level: 3
  34. },
  35. children: [
  36. {
  37. actionType: 'toast',
  38. args: {
  39. msg: '第1层循环动作1${name}'
  40. },
  41. preventDefault: false,
  42. stopPropagation: false
  43. },
  44. {
  45. actionType: 'toast',
  46. args: {
  47. msg: '第1层循环动作2${name}'
  48. }
  49. },
  50. {
  51. actionType: 'loop',
  52. args: {
  53. loopName: '${loopName}',
  54. level: 3
  55. },
  56. children: [
  57. {
  58. actionType: 'toast',
  59. args: {
  60. msg: '第2层循环动作1${name}'
  61. }
  62. },
  63. {
  64. actionType: 'toast',
  65. args: {
  66. msg: '第2层循环动作2${name}'
  67. },
  68. preventDefault: false,
  69. stopPropagation: false
  70. },
  71. {
  72. actionType: 'continue'
  73. },
  74. {
  75. actionType: 'toast',
  76. args: {
  77. msg: '第2层循环动作3${name}'
  78. }
  79. }
  80. ]
  81. }
  82. ]
  83. }
  84. ]
  85. }
  86. }
  87. }
  88. ]
  89. }
  90. }

动作属性

属性名 类型 默认值 说明
actionType string for 循环执行动作
args.loopName string - 循环变量名称,< 1.8.0 及以下版本 为 loopName
children Array<动作> - 子动作,可以通过break动作来跳出循环

Break 动作

  1. {
  2. type: 'page',
  3. body: {
  4. type: 'form',
  5. wrapWithPanel: false,
  6. data: {
  7. loopName: 'loopData',
  8. loopData: [
  9. {
  10. name: 'lv',
  11. age: '19'
  12. },
  13. {
  14. name: 'xj',
  15. age: '21'
  16. }
  17. ]
  18. },
  19. body: [
  20. {
  21. type: 'button',
  22. label: '只执行了第一个动作就跳出了循环',
  23. level: 'primary',
  24. onEvent: {
  25. click: {
  26. actions: [
  27. {
  28. actionType: 'loop',
  29. args: {
  30. loopName: '${loopName}'
  31. },
  32. children: [
  33. {
  34. actionType: "toast",
  35. args: {
  36. msgType: 'success',
  37. msg: '第 1 个动作',
  38. position: 'top-right'
  39. }
  40. },
  41. {
  42. actionType: 'break'
  43. },
  44. {
  45. actionType: "toast",
  46. args: {
  47. msgType: 'success',
  48. msg: '第 2 个动作',
  49. position: 'top-right'
  50. }
  51. }
  52. ]
  53. }
  54. ]
  55. }
  56. }
  57. }
  58. ]
  59. }
  60. }

动作属性

属性名 类型 默认值 说明
actionType string break 跳出循环动作

Continue 动作

  1. {
  2. type: 'page',
  3. body: {
  4. type: 'form',
  5. wrapWithPanel: false,
  6. data: {
  7. loopName: 'loopData',
  8. loopData: [
  9. {
  10. name: 'lv',
  11. age: '19'
  12. },
  13. {
  14. name: 'xj',
  15. age: '21'
  16. }
  17. ]
  18. },
  19. body: [
  20. {
  21. type: 'button',
  22. label: '只循环执行第一个动作',
  23. level: 'primary',
  24. onEvent: {
  25. click: {
  26. actions: [
  27. {
  28. actionType: 'loop',
  29. args: {
  30. loopName: '${loopName}',
  31. level: 3
  32. },
  33. children: [
  34. {
  35. actionType: "toast",
  36. args: {
  37. msgType: 'success',
  38. msg: '第 1 个动作',
  39. position: 'top-right'
  40. }
  41. },
  42. {
  43. actionType: 'continue'
  44. },
  45. {
  46. actionType: "toast",
  47. args: {
  48. msgType: 'success',
  49. msg: '最后的动作',
  50. position: 'top-right'
  51. }
  52. }
  53. ]
  54. }
  55. ]
  56. }
  57. }
  58. }
  59. ]
  60. }
  61. }

动作属性

属性名 类型 默认值 说明
actionType string continue 跳出当前

排他(switch)

  1. {
  2. type: 'page',
  3. body: {
  4. type: 'form',
  5. wrapWithPanel: false,
  6. data: {
  7. branchCont: 18
  8. },
  9. body: [
  10. {
  11. type: 'button',
  12. label: '只执行动作2',
  13. level: 'primary',
  14. onEvent: {
  15. click: {
  16. actions: [
  17. {
  18. actionType: 'switch',
  19. children: [
  20. {
  21. actionType: "toast",
  22. args: {
  23. msgType: 'info',
  24. msg: '动作1',
  25. position: 'top-right'
  26. },
  27. expression: 'this.branchCont > 19',
  28. stopPropagation: true // 这里无效,因为条件不成立
  29. },
  30. {
  31. actionType: "toast",
  32. args: {
  33. msgType: 'info',
  34. msg: '动作2',
  35. position: 'top-right'
  36. },
  37. expression: 'this.branchCont > 17'
  38. },
  39. {
  40. actionType: "toast",
  41. args: {
  42. msgType: 'info',
  43. msg: '动作3',
  44. position: 'top-right'
  45. },
  46. expression: 'this.branchCont > 16'
  47. }
  48. ]
  49. }
  50. ]
  51. }
  52. }
  53. }
  54. ]
  55. }
  56. }

动作属性

属性名 类型 默认值 说明
actionType string switch 只执行第一个符合条件的动作
children Array<动作> - 子动作,每个子动作可以通过配置expression来匹配的条件

并行

  1. {
  2. type: 'page',
  3. body: {
  4. type: 'form',
  5. wrapWithPanel: false,
  6. body: [
  7. {
  8. type: 'button',
  9. label: '同时发送两个ajax请求,并显示请求返回',
  10. level: 'primary',
  11. onEvent: {
  12. click: {
  13. actions: [
  14. {
  15. actionType: 'parallel',
  16. children: [
  17. {
  18. actionType: 'ajax',
  19. args: {
  20. api: {
  21. url: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/initData?name=${name}',
  22. method: 'get'
  23. },
  24. messages: {
  25. success: '请求1成功了!欧耶',
  26. failed: '失败了呢。。'
  27. }
  28. },
  29. outputVar: 'var1'
  30. },
  31. {
  32. actionType: 'ajax',
  33. args: {
  34. api: {
  35. url: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm?name=${name}',
  36. method: 'get'
  37. },
  38. messages: {
  39. success: '请求2成功了!欧耶',
  40. failed: '失败了呢。。'
  41. }
  42. },
  43. outputVar: 'var2'
  44. }
  45. ]
  46. },
  47. {
  48. actionType: 'toast',
  49. args: {
  50. msg: 'var1:${event.data.var1|json}, var2:${event.data.var2|json}'
  51. }
  52. }
  53. ]
  54. }
  55. }
  56. }
  57. ]
  58. }
  59. }

动作属性

属性名 类型 默认值 说明
actionType string parallel 并行执行多个动作
children Array<动作> - 子动作

动作间数据传递

从事件触发开始,整个数据流包含事件本身产生的事件数据和动作产生的动作数据,事件源头产生的数据在 AMIS 事件动作机制底层已经自动加入渲染器数据域,可以通过event.data.xxx直接获取,而部分动作产生的数据如何流动需要交互设计者进行介入,对于数据流动可以通过数据映射,将上一个动作产生的数据作为动作参数写入下一个动作。

传递数据

通过 args 指定输入的参数数据,它是一个键值对。

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. id: 'b_001',
  7. label: '发一个广播,携带动作参数',
  8. className: 'mb-2',
  9. level: 'primary',
  10. onEvent: {
  11. click: {
  12. actions: [
  13. {
  14. actionType: 'broadcast',
  15. eventName: 'broadcast_1',
  16. args: {
  17. name: 'lvxj',
  18. age: 18
  19. },
  20. description: '一个按钮的点击事件'
  21. }
  22. ]
  23. }
  24. }
  25. },
  26. {
  27. type: 'form',
  28. name: 'form1',
  29. id: 'form_001',
  30. title: '接收广播事件的参数',
  31. debug: true,
  32. body: [
  33. {
  34. type: 'input-text',
  35. id: 'form_001_text_01',
  36. label: '年龄',
  37. name: 'age',
  38. disabled: false,
  39. mode: 'horizontal'
  40. }
  41. ],
  42. data: {
  43. name: 'amis'
  44. },
  45. onEvent: {
  46. broadcast_1: {
  47. actions: [
  48. {
  49. actionType: 'reload',
  50. args: {
  51. age: '${event.data.age}'
  52. }
  53. }
  54. ]
  55. }
  56. }
  57. }
  58. ]
  59. }

存储异步请求返回的数据

通过 outputVar 指定输出的变量名,其他动作可以通过${event.data.{{outputVar}}}来获取变量值,如果未指定 outputVar ,则直接存储到event.data

  1. {
  2. type: 'page',
  3. body: [
  4. {
  5. type: 'button',
  6. label: '发送Ajax请求,并把返回数据传给弹窗',
  7. level: 'primary',
  8. onEvent: {
  9. click: {
  10. actions: [
  11. {
  12. actionType: 'ajax',
  13. args: {
  14. api: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm',
  15. messages: {
  16. success: '成功了!欧耶',
  17. failed: '失败了呢。。'
  18. }
  19. },
  20. outputVar: 'ajax1'
  21. },
  22. {
  23. actionType: 'dialog',
  24. args: {
  25. id: '${event.data.ajax1.id}'
  26. },
  27. dialog: {
  28. type: 'dialog',
  29. id: 'dialog_005',
  30. title: '弹框标题1',
  31. data: {
  32. id: '${id}'
  33. },
  34. body: [
  35. {
  36. type: 'form',
  37. body: [
  38. {
  39. type: 'tpl',
  40. tpl: '<p>请求返回的数据:id=${id}</p>',
  41. inline: false
  42. }
  43. ]
  44. }
  45. ]
  46. }
  47. }
  48. ]
  49. }
  50. }
  51. }
  52. ]
  53. }

事件动作干预

事件动作干预是指执行完当前动作后,干预所监听事件默认处理逻辑和后续其他动作的执行。通过preventDefaultstopPropagation分别阻止监听事件默认行为和停止下一个动作执行。

阻止事件默认行为

有些组件内置了一些逻辑来帮助用户降低配置成本,但可能这些逻辑并不符合设计者的业务需求,这时可以通过onEvent来监听对应的事件,并通过preventDefault来阻止那些默认处理逻辑来达到想要的最终效果。更多示例请查看阻止组件默认行为示例

  1. {
  2. type: 'page',
  3. title: '弹窗确认后执行其他动作并阻止默认关闭',
  4. body: [
  5. {
  6. type: 'button',
  7. className: 'ml-2',
  8. label: '打开弹窗',
  9. level: 'primary',
  10. onEvent: {
  11. click: {
  12. actions: [
  13. {
  14. actionType: 'dialog',
  15. dialog: {
  16. type: 'dialog',
  17. title: '提示',
  18. id: 'dialog_001',
  19. data: {
  20. myage: '22'
  21. },
  22. body: [
  23. {
  24. type: 'alert',
  25. body: '输入Do not close,确认后将不关闭弹窗',
  26. level: 'warning'
  27. },
  28. {
  29. type: 'input-text',
  30. name: 'command'
  31. }
  32. ],
  33. onEvent: {
  34. confirm: {
  35. actions: [
  36. {
  37. actionType: 'toast',
  38. args: {
  39. msg: '不关闭'
  40. },
  41. preventDefault: 'event.data.command === "Do not close"'
  42. }
  43. ]
  44. }
  45. }
  46. }
  47. }
  48. ]
  49. }
  50. }
  51. }
  52. ]
  53. }

停止后续动作执行

通过onEvent可以对监听的事件配置一组动作,这些动作是顺序执行的,有时间设计者希望执行某个/些动作后就停止继续执行后面的动作,这时候可以通过stopPropagation来停止执行后面配置的所有动作。

  1. {
  2. "type": "page",
  3. "title": "只执行3个动作中的前两个动作",
  4. "body": [
  5. {
  6. "type": "button",
  7. "label": "弹出2个提示",
  8. level: 'primary',
  9. "onEvent": {
  10. "click": {
  11. "actions": [
  12. {
  13. "actionType": "toast",
  14. args: {
  15. "msgType": 'info',
  16. "msg": '动作1'
  17. }
  18. },
  19. {
  20. "actionType": "toast",
  21. args: {
  22. "msgType": 'info',
  23. "msg": '动作2'
  24. },
  25. "stopPropagation": true
  26. },
  27. {
  28. "actionType": "toast",
  29. args: {
  30. "msgType": 'info',
  31. "msg": '动作3',
  32. "position": 'top-right'
  33. }
  34. }
  35. ]
  36. }
  37. }
  38. }
  39. ]
  40. }

自定义组件接入事件动作

需求场景主要是想要自定义组件的内部事件暴露出去,能够通过对事件的监听来执行所需动作,并希望自定义组件自身的动作能够被其他组件调用。接入方法是通过props.dispatchEvent派发自身的各种事件,使其具备更灵活的交互设计能力;通过实现doAction方法实现其他组件对其专属动作的调用,需要注意的是,此处依赖内部的 Scoped Context来实现自身的注册,可以参考 组件间通信

属性表

属性名 类型 默认值 说明
actionType string - 动作名称
args object - 动作参数{key:value},支持数据映射
preventDefault boolean\ 表达式 false 阻止事件默认行为,> 1.10.0 及以上版本支持表达式
stopPropagation boolean\ 表达式 false 停止后续动作执行,> 1.10.0 及以上版本支持表达式
expression boolean\ 表达式 - 执行条件,不设置表示默认执行
outputVar string - 输出数据变量名