Skip to content

Commit

Permalink
feat: 处理业务异常情况 (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
PPPenny committed Jul 31, 2020
1 parent ff42a28 commit 644e9a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
44 changes: 44 additions & 0 deletions docs/throw-custom-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

throw-custom-error

```vue
<template>
<div style="height: 400px; overflow: auto">
<data-list ref="dataList" v-bind="listConfig" @error="error">
<!--通过slot-scope从data-list组件获取到返回的数据-->
<!-- get list itme data via slot, so you can customize rendering-->
<ul slot-scope="props">
<li v-for="(item, index) in props.list" :key="index">
{{item.name}}
</li>
</ul>
<div slot="error">{{errorMsg}}</div>
</data-list>
</div>
</template>
<script>
import _get from 'lodash.get'
export default {
data() {
return {
listConfig:{
url: 'https://mockapi.eolinker.com/KNhMPAkb444ac06613fc8d63795be9ad0beaf55011936ac/data-list',
throwCustomError:this.throwCustomError
},
errorMsg:''
}
},
methods:{
error(e){
this.errorMsg = e.message
},
throwCustomError(resp){
if(!_get(resp, 'payload')){
throw resp
}
}
}
}
</script>
```

10 changes: 9 additions & 1 deletion src/data-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ export default {
saveQuery: {
type: Boolean,
default: true
},
/**
* error 特殊处理异常情况
*/
throwCustomError:{
type: Function,
default: null
}
},
data() {
Expand Down Expand Up @@ -195,7 +202,8 @@ export default {
try {
const {data: resp} = await this.$axios.get(url + queryStr)
// 处理业务异常情况
this.throwCustomError && this.throwCustomError(resp)
// 当读取结果为undefined时取默认值[]
const data = _get(resp, this.dataPath, [])
const action = isDirectionDown ? 'push' : 'unshift'
Expand Down

0 comments on commit 644e9a2

Please sign in to comment.