Skip to content

Commit 585c61d

Browse files
authored
feat: Service组件支持隐藏错误提示 (#6431)
1 parent 8c13e72 commit 585c61d

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

docs/zh-CN/components/service.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,45 @@ ws.on('connection', function connection(ws) {
677677
}
678678
```
679679

680+
## 隐藏错误信息
681+
682+
> 2.8.1 及以上版本
683+
684+
默认会将接口返回的错误信息展示在 Service 的顶部区域,可以通过设置`"showErrorMsg": false`隐藏错误提示。
685+
686+
```schema: scope="body"
687+
{
688+
"type": "service",
689+
"api": "/api/mock2/page/initDataError",
690+
"body": [
691+
{
692+
"type": "tpl",
693+
"tpl": "展示错误信息"
694+
},
695+
{
696+
"type": "icon",
697+
"icon": "fa-solid fa-arrow-up"
698+
}
699+
]
700+
}
701+
```
702+
703+
设置`"showErrorMsg": false`隐藏错误提示,仅保留 toast 提示
704+
705+
```schema: scope="body"
706+
{
707+
"type": "service",
708+
"api": "/api/mock2/page/initDataError",
709+
"showErrorMsg": false,
710+
"body": [
711+
{
712+
"type": "tpl",
713+
"tpl": "不展示错误信息"
714+
}
715+
]
716+
}
717+
```
718+
680719
## 属性表
681720

682721
| 属性名 | 类型 | 默认值 | 说明 | 版本 |
@@ -696,6 +735,7 @@ ws.on('connection', function connection(ws) {
696735
| interval | `number` | | 轮询时间间隔,单位 ms(最低 1000) |
697736
| silentPolling | `boolean` | `false` | 配置轮询时是否显示加载动画 |
698737
| stopAutoRefreshWhen | [表达式](../../docs/concepts/expression) | | 配置停止轮询的条件 |
738+
| showErrorMsg | `boolean` | `true` | 是否以Alert的形式显示api接口响应的错误信息,默认展示 | `2.8.1` |
699739

700740
## 事件表
701741

packages/amis/src/renderers/Service.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
isEffectiveApi,
1313
str2AsyncFunction
1414
} from 'amis-core';
15-
import {Spinner, SpinnerExtraProps} from 'amis-ui';
15+
import {Spinner, SpinnerExtraProps, Alert2 as Alert} from 'amis-ui';
1616
import {
1717
autobind,
1818
isObject,
@@ -138,6 +138,11 @@ export interface ServiceSchema extends BaseSchema, SpinnerExtraProps {
138138
messages?: SchemaMessage;
139139

140140
name?: SchemaName;
141+
142+
/**
143+
* 是否以Alert的形式显示api接口响应的错误信息,默认展示
144+
*/
145+
showErrorMsg?: boolean;
141146
}
142147

143148
export interface ServiceProps
@@ -162,7 +167,8 @@ export default class Service extends React.Component<ServiceProps> {
162167
static defaultProps: Partial<ServiceProps> = {
163168
messages: {
164169
fetchFailed: 'fetchFailed'
165-
}
170+
},
171+
showErrorMsg: true
166172
};
167173

168174
static propsList: Array<string> = [];
@@ -736,22 +742,20 @@ export default class Service extends React.Component<ServiceProps> {
736742
render,
737743
classPrefix: ns,
738744
classnames: cx,
739-
loadingConfig
745+
loadingConfig,
746+
showErrorMsg
740747
} = this.props;
741748

742749
return (
743750
<div className={cx(`${ns}Service`, className)} style={style}>
744-
{store.error ? (
745-
<div className={cx(`Alert Alert--danger`)}>
746-
<button
747-
className={cx('Alert-close')}
748-
onClick={() => store.updateMessage('')}
749-
type="button"
750-
>
751-
<span>×</span>
752-
</button>
751+
{store.error && showErrorMsg !== false ? (
752+
<Alert
753+
level="danger"
754+
showCloseButton
755+
onClose={() => store.updateMessage('')}
756+
>
753757
{store.msg}
754-
</div>
758+
</Alert>
755759
) : null}
756760

757761
{this.renderBody()}

0 commit comments

Comments
 (0)