Skip to content

Commit eb2e225

Browse files
committed
add hook(useBoundary)
1 parent 27b9e65 commit eb2e225

23 files changed

+135
-27
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ import FallbackComponent, from './fallback.vue';
207207
You can view the examples used with suspend+vue-query in the
208208
[codesandbox](https://codesandbox.io/s/pcmg9e)
209209

210+
## useBoundary
211+
212+
It is not necessary to obtain reset and error through props,more convenient to use hook.
213+
214+
```ts
215+
216+
const {error, reset} = useBoundary();
217+
console.log(error?.message, error?.name);
218+
...
219+
220+
```
221+
210222
# Devtools
211223

212224
Support Vue devtools.You can view the error information and other contents in the developer tool.

__tests__/capture.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mount } from '@vue/test-utils';
2-
2+
import { describe, test, beforeEach, expect } from 'vitest';
33
import ClickThrow from './components/click.vue';
44
import Caputre from './components/capture.vue';
55
import ErrorBoundary, { ErrorBoundaryProps } from '@src';

__tests__/components/capture.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defineProps<{
77

88
<template>
99
<p id="error">{{ error.name }},{{ error.message }}</p>
10-
<button @click="reset" id="reset">Reset</button>
10+
<button id="reset" @click="reset">Reset</button>
1111
</template>
1212

1313
<script lang="ts">

__tests__/components/click.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ function throwError() {
55
</script>
66

77
<template>
8-
<button @click="throwError" id="throw">throw error</button>
8+
<button id="throw" @click="throwError">throw error</button>
99
</template>
10+
11+
<script lang="ts">
12+
export default {
13+
name: 'ClickTestComponent',
14+
};
15+
</script>

__tests__/components/hook.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script setup lang="ts">
2+
import { useBoundary } from '@src';
3+
4+
const { reset, error } = useBoundary();
5+
</script>
6+
7+
<template>
8+
<p id="error_info">{{ error?.message ?? '' }}</p>
9+
<button id="reset_btn" @click="reset">reset</button>
10+
</template>
11+
12+
<script lang="ts">
13+
export default {
14+
name: 'HookTestComponent',
15+
};
16+
</script>

__tests__/components/network.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import { useQuery } from 'vue-query';
33
import axios from 'axios';
4+
45
const props = defineProps<{ returnError: boolean }>();
56
67
const { data, suspense } = useQuery(
@@ -17,9 +18,8 @@ const { data, suspense } = useQuery(
1718
1819
if (data.status === 'success') {
1920
return data.data;
20-
} else {
21-
throw new Error(data.data);
2221
}
22+
throw new Error(data.data);
2323
},
2424
{ retry: false },
2525
);

__tests__/components/refError.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function throwError() {
55
</script>
66

77
<template>
8-
<button @click="throwError" id="ref_error">ref error</button>
8+
<button id="ref_error" @click="throwError">ref error</button>
99
</template>
1010

1111
<script lang="ts">

__tests__/components/typeError.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function throwError() {
55
</script>
66

77
<template>
8-
<button @click="throwError" id="type_error">type error</button>
8+
<button id="type_error" @click="throwError">type error</button>
99
</template>
1010

1111
<script lang="ts">

__tests__/define.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ErrorBoundary from '@src';
2+
import { describe, test, expect } from 'vitest';
23

34
describe('VueErrorBoundary is define', function () {
45
test('VueErrorBoundary is define', function () {

__tests__/emits.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ClickThrow from './components/click.vue';
77
import Caputre from './components/capture.vue';
88
import { mount } from '@vue/test-utils';
99
import { defineComponent, h } from 'vue';
10+
import { describe, test, expect } from 'vitest';
1011

1112
const App = defineComponent({
1213
emits: ['captured'],

0 commit comments

Comments
 (0)