配置与组件

最简单的 amis 配置

一个最简单的 amis 配置看起来是这样的:

  1. {
  2. "type": "page",
  3. "body": "Hello World!"
  4. }

请观察上面的代码,这是一段 JSON,它的含义是:

  1. type 是 amis 节点中最重要的字段,它告诉 amis 当前节点需要渲染的是Page组件。
  2. body 字段会作为 Page 组件的属性,Page 组件根据这个值来渲染页面内容。

这段配置的效果如下所示:

  1. {
  2. "type": "page",
  3. "body": "Hello World!"
  4. }

上面这个配置是可以实时修改预览的,尝试改一下 Hello World! 的值。

不过这个实时预览功能对于某些属性不生效,如果发现不符合预期,需要复制 JSON,打开另一个页面后粘贴。

组件

上面提到,type字段会告诉 amis 当前节点渲染的组件为Page,组件节点的配置永远都是由 type字段 (用于标识当前是哪个组件)和 属性 构成的。

  1. {
  2. "type": "xxx",
  3. ...其它属性
  4. }

组件树

这次我们看一个稍微复杂一点的配置:

  1. {
  2. "type": "page",
  3. "body": {
  4. "type": "tpl",
  5. "tpl": "Hello World!"
  6. }
  7. }

该配置渲染页面如下:

  1. {
  2. "type": "page",
  3. "body": {
  4. "type": "tpl",
  5. "tpl": "Hello World!"
  6. }
  7. }

最终效果和前面的示例一样,但这次 Page 组件的 body 属性值配置了一个对象,通过type指明body内容区内会渲染一个叫Tpl的组件,它是一个模板渲染组件。

body 中除了配置对象,还可以是数组,比如下面的例子:

```schema: scope=”body” [ { “type”: “tpl”, “tpl”: “Hello World!” }, { “type”: “divider” }, { “type”: “form”, “body”: [ { “type”: “input-text”, “name”: “name”, “label”: “姓名” } ] } ]

  1. 可以看到通过数组的形式,增加了 `divider` `form` 组件。
  2. 除了 `Page` 之外,还有很多**容器型**的组件都有 `body` 属性,通过这种树形结构,amis 就能实现复杂页面制作。
  3. > **注意:**
  4. >
  5. > `Page` amis 页面配置中 **必须也是唯一的顶级节点**
  6. ### 通过树形来实现布局
  7. 下面这个页面就是通过树形组合出来的,大体结构是这样:

Page ├── Toolbar │ └─ Form 顶部表单项 ├── Grid // 用于水平布局 │ ├─ Panel │ │ └─ Tabs │ │ └─ Chart │ └─ Panel │ └─ Chart └── CRUD

  1. ```schema
  2. {
  3. "type": "page",
  4. "toolbar": [{
  5. "type": "form",
  6. "panelClassName": "mb-0",
  7. "title": "",
  8. "body": [{
  9. "type": "select",
  10. "label": "区域",
  11. "name": "businessLineId",
  12. "selectFirst": true,
  13. "mode": "inline",
  14. "options": ["北京", "上海"],
  15. "checkAll": false
  16. }, {
  17. "label": "时间范围",
  18. "type": "input-date-range",
  19. "name": "dateRange",
  20. "inline": true,
  21. "value": "-1month,+0month",
  22. "inputFormat": "YYYY-MM-DD",
  23. "format": "YYYY-MM-DD",
  24. "closeOnSelect": true,
  25. "clearable": false
  26. }],
  27. "actions": [],
  28. "mode": "inline",
  29. "target": "mainPage",
  30. "submitOnChange": true,
  31. "submitOnInit": true
  32. }],
  33. "body": [{
  34. "type": "grid",
  35. "columns": [
  36. {
  37. "type": "panel",
  38. "className": "h-full",
  39. "body": {
  40. "type": "tabs",
  41. "tabs": [{
  42. "title": "消费趋势",
  43. "tab": [{
  44. "type": "chart",
  45. "config": {
  46. "title": {
  47. "text": "消费趋势"
  48. },
  49. "tooltip": {},
  50. "xAxis": {
  51. "type": "category",
  52. "boundaryGap": false,
  53. "data": ["一月", "二月", "三月", "四月", "五月", "六月"]
  54. },
  55. "yAxis": {},
  56. "series": [{
  57. "name": "销量",
  58. "type": "line",
  59. "areaStyle": {
  60. "color": {
  61. "type": "linear",
  62. "x": 0,
  63. "y": 0,
  64. "x2": 0,
  65. "y2": 1,
  66. "colorStops": [{
  67. "offset": 0,
  68. "color": "rgba(84, 112, 197, 1)"
  69. }, {
  70. "offset": 1,
  71. "color": "rgba(84, 112, 197, 0)"
  72. }],
  73. "global": false
  74. }
  75. },
  76. "data": [5, 20, 36, 10, 10, 20]
  77. }]
  78. }
  79. }]
  80. }, {
  81. "title": "账户余额",
  82. "tab": "0"
  83. }]
  84. }
  85. }, {
  86. "type": "panel",
  87. "className": "h-full",
  88. "body": [{
  89. "type": "chart",
  90. "config": {
  91. "title": {
  92. "text": "使用资源占比"
  93. },
  94. "series": [{
  95. "type": "pie",
  96. "data": [{
  97. "name": "BOS",
  98. "value": 70
  99. }, {
  100. "name": "CDN",
  101. "value": 68
  102. }, {
  103. "name": "BCC",
  104. "value": 48
  105. }, {
  106. "name": "DCC",
  107. "value": 40
  108. }, {
  109. "name": "RDS",
  110. "value": 32
  111. }]
  112. }]
  113. }
  114. }]
  115. }]
  116. }, {
  117. "type": "crud",
  118. "className": "m-t-sm",
  119. "api": "/api/mock2/sample",
  120. "columns": [{
  121. "name": "id",
  122. "label": "ID"
  123. }, {
  124. "name": "engine",
  125. "label": "Rendering engine"
  126. }, {
  127. "name": "browser",
  128. "label": "Browser"
  129. }, {
  130. "name": "platform",
  131. "label": "Platform(s)"
  132. }, {
  133. "name": "version",
  134. "label": "Engine version"
  135. }, {
  136. "name": "grade",
  137. "label": "CSS grade"
  138. }]
  139. }]
  140. }

amis 后续将会实现新的布局模式,将更容易实现复杂布局效果