feat: Service组件支持隐藏错误提示 (#6431)

This commit is contained in:
RUNZE LU 2023-03-21 20:10:00 +08:00 committed by GitHub
parent 8c13e7202b
commit 585c61df6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 13 deletions

View File

@ -677,6 +677,45 @@ ws.on('connection', function connection(ws) {
} }
``` ```
## 隐藏错误信息
> 2.8.1 及以上版本
默认会将接口返回的错误信息展示在 Service 的顶部区域,可以通过设置`"showErrorMsg": false`隐藏错误提示。
```schema: scope="body"
{
"type": "service",
"api": "/api/mock2/page/initDataError",
"body": [
{
"type": "tpl",
"tpl": "展示错误信息"
},
{
"type": "icon",
"icon": "fa-solid fa-arrow-up"
}
]
}
```
设置`"showErrorMsg": false`隐藏错误提示,仅保留 toast 提示
```schema: scope="body"
{
"type": "service",
"api": "/api/mock2/page/initDataError",
"showErrorMsg": false,
"body": [
{
"type": "tpl",
"tpl": "不展示错误信息"
}
]
}
```
## 属性表 ## 属性表
| 属性名 | 类型 | 默认值 | 说明 | 版本 | | 属性名 | 类型 | 默认值 | 说明 | 版本 |
@ -696,6 +735,7 @@ ws.on('connection', function connection(ws) {
| interval | `number` | | 轮询时间间隔,单位 ms(最低 1000) | | interval | `number` | | 轮询时间间隔,单位 ms(最低 1000) |
| silentPolling | `boolean` | `false` | 配置轮询时是否显示加载动画 | | silentPolling | `boolean` | `false` | 配置轮询时是否显示加载动画 |
| stopAutoRefreshWhen | [表达式](../../docs/concepts/expression) | | 配置停止轮询的条件 | | stopAutoRefreshWhen | [表达式](../../docs/concepts/expression) | | 配置停止轮询的条件 |
| showErrorMsg | `boolean` | `true` | 是否以Alert的形式显示api接口响应的错误信息默认展示 | `2.8.1` |
## 事件表 ## 事件表

View File

@ -12,7 +12,7 @@ import {
isEffectiveApi, isEffectiveApi,
str2AsyncFunction str2AsyncFunction
} from 'amis-core'; } from 'amis-core';
import {Spinner, SpinnerExtraProps} from 'amis-ui'; import {Spinner, SpinnerExtraProps, Alert2 as Alert} from 'amis-ui';
import { import {
autobind, autobind,
isObject, isObject,
@ -138,6 +138,11 @@ export interface ServiceSchema extends BaseSchema, SpinnerExtraProps {
messages?: SchemaMessage; messages?: SchemaMessage;
name?: SchemaName; name?: SchemaName;
/**
* Alert的形式显示api接口响应的错误信息
*/
showErrorMsg?: boolean;
} }
export interface ServiceProps export interface ServiceProps
@ -162,7 +167,8 @@ export default class Service extends React.Component<ServiceProps> {
static defaultProps: Partial<ServiceProps> = { static defaultProps: Partial<ServiceProps> = {
messages: { messages: {
fetchFailed: 'fetchFailed' fetchFailed: 'fetchFailed'
} },
showErrorMsg: true
}; };
static propsList: Array<string> = []; static propsList: Array<string> = [];
@ -736,22 +742,20 @@ export default class Service extends React.Component<ServiceProps> {
render, render,
classPrefix: ns, classPrefix: ns,
classnames: cx, classnames: cx,
loadingConfig loadingConfig,
showErrorMsg
} = this.props; } = this.props;
return ( return (
<div className={cx(`${ns}Service`, className)} style={style}> <div className={cx(`${ns}Service`, className)} style={style}>
{store.error ? ( {store.error && showErrorMsg !== false ? (
<div className={cx(`Alert Alert--danger`)}> <Alert
<button level="danger"
className={cx('Alert-close')} showCloseButton
onClick={() => store.updateMessage('')} onClose={() => store.updateMessage('')}
type="button" >
>
<span>×</span>
</button>
{store.msg} {store.msg}
</div> </Alert>
) : null} ) : null}
{this.renderBody()} {this.renderBody()}