Skip to content

Commit 678644b

Browse files
committed
1-7 Utilizando hook na prática
1 parent 47b84aa commit 678644b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

public/data/nomes.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{ "id": 1, "name": "Alice" },
3+
{ "id": 2, "name": "Bob" },
4+
{ "id": 3, "name": "Carol" },
5+
{ "id": 4, "name": "David" }
6+
]

src/App.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ import Texto from "./components/Texto.vue";
55
const mensagem = ref("");
66
const contador = ref(0);
77
const showComponent = ref(true);
8+
const items = ref([]);
9+
10+
const fetchData = async () => {
11+
try {
12+
const response = await fetch("../public/data/nomes.json");
13+
const data = await response.json();
14+
items.value = data;
15+
} catch (error) {
16+
console.error("O erro é: ", error);
17+
}
18+
};
819
920
function toggleComponent() {
1021
showComponent.value = !showComponent.value;
@@ -18,6 +29,7 @@ function incrementar() {
1829
onMounted(() => {
1930
mensagem.value = "Componente Montado";
2031
console.log("onMounted executado");
32+
fetchData();
2133
});
2234
2335
// Antes de ser montado
@@ -43,4 +55,9 @@ onBeforeUpdate(() => {
4355
<br />
4456
<button @click="toggleComponent">Mostrar/Esconder</button>
4557
<Texto v-if="showComponent" />
58+
59+
<div v-if="items.length === 0">Carregando...</div>
60+
<ul v-else>
61+
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
62+
</ul>
4663
</template>

0 commit comments

Comments
 (0)