Skip to content

Commit 55d9a9b

Browse files
committed
suspense topic
1 parent b97cb82 commit 55d9a9b

File tree

4 files changed

+54
-35
lines changed

4 files changed

+54
-35
lines changed

src/components/slots/BaseCard.vue

+21-25
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
11
<template>
22
<div class="card shadow-none p-3 m-5 rounded ">
3-
<div class="card-header" v-if="$slots.default">
4-
<slot></slot>
5-
</div>
6-
<div class="card-body">
3+
<div class="card-header" v-if="$slots.default">
4+
<slot></slot>
5+
</div>
6+
<div class="card-body">
77
<slot name="body">
88
<div class="alert alert-danger">
99
default content
1010
</div>
1111
</slot>
12-
</div>
13-
14-
15-
<div class="card-footer" v-if="$slots.footer">
16-
<slot name="footer" :count="count"></slot>
17-
</div>
12+
</div>
13+
<div class="card-footer" v-if="$slots.footer">
14+
<slot name="footer" :count="count"></slot>
15+
</div>
1816
</div>
19-
</template>
20-
21-
<script setup>
17+
</template>
18+
19+
<script setup>
2220
import { ref } from 'vue';
2321
2422
25-
const count = ref(0);
23+
const count = ref(0);
2624
27-
const id = setInterval(() => {
28-
count.value = count.value + 10;
25+
const id = setInterval(() => {
26+
count.value = count.value + 10;
2927
30-
if(count.value > 300){
28+
if (count.value > 300) {
3129
clearInterval(id);
3230
}
33-
34-
}, 3000);
35-
36-
</script>
37-
38-
<style>
39-
40-
</style>
31+
32+
}, 3000);
33+
34+
</script>
35+
36+
<style></style>

src/components/slots/SlotChild.vue

-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<template>
22
<div class="container">
33
<BaseCardVue>
4-
5-
6-
7-
84
<template #[slotName]>
95
<h1>card content</h1>
106
<h2>{{ username }}</h2>

src/components/suspense/UserDashboard.vue

+13-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ref } from 'vue'
1515
// Simulating API calls
1616
1717
const fetchUser = () => {
18-
return new Promise((reslove) => {
18+
return new Promise((reslove,reject) => {
1919
setTimeout(() => {
2020
reslove({ id: 1, name: "naresh jupalle", email: "naresh.jupalle410@gmail.com" });
2121
}, 1000);
@@ -31,8 +31,16 @@ const fetchPosts = (userId) => new Promise(resolve =>
3131
3232
// Async setup
3333
34-
console.log(await fetchUser())
35-
console.log(await fetchPosts())
36-
const user = ref(await fetchUser())
37-
const posts = ref(await fetchPosts(user.value.id))
34+
// console.log(await fetchUser())
35+
// console.log(await fetchPosts())
36+
const user = ref(null);
37+
const posts = ref(null)
38+
try {
39+
user.value = await fetchUser()
40+
posts.value = await fetchPosts(user.value.id)
41+
}catch(e) {
42+
console.log("an error occured")
43+
console.log(e);
44+
}
45+
3846
</script>

src/components/suspense/vueSuspens.vue

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22

3-
<Suspense>
3+
<!-- @error-captured="handleError" -->
4+
<Suspense >
45
<template #default>
56
<user-dashboard></user-dashboard>
67
</template>
@@ -14,4 +15,22 @@
1415

1516
<script setup>
1617
import UserDashboard from './UserDashboard.vue';
18+
import { onErrorCaptured } from 'vue';
19+
20+
21+
// one way of doing
22+
// onErrorCaptured((error) => {
23+
// debugger;
24+
// console.log(error);
25+
// })
26+
27+
// second listening event on suspense tag.
28+
29+
// function handleError(error) {
30+
// debugger
31+
// console.log(error)
32+
// }
33+
34+
35+
1736
</script>

0 commit comments

Comments
 (0)