Skip to content

Commit

Permalink
1-7 Utilizando hook na prática
Browse files Browse the repository at this point in the history
  • Loading branch information
AmauriBlanco committed Nov 5, 2024
1 parent 47b84aa commit 678644b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions public/data/nomes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" },
{ "id": 3, "name": "Carol" },
{ "id": 4, "name": "David" }
]
17 changes: 17 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import Texto from "./components/Texto.vue";
const mensagem = ref("");
const contador = ref(0);
const showComponent = ref(true);
const items = ref([]);
const fetchData = async () => {
try {
const response = await fetch("../public/data/nomes.json");
const data = await response.json();
items.value = data;
} catch (error) {
console.error("O erro é: ", error);
}
};
function toggleComponent() {
showComponent.value = !showComponent.value;
Expand All @@ -18,6 +29,7 @@ function incrementar() {
onMounted(() => {
mensagem.value = "Componente Montado";
console.log("onMounted executado");
fetchData();
});
// Antes de ser montado
Expand All @@ -43,4 +55,9 @@ onBeforeUpdate(() => {
<br />
<button @click="toggleComponent">Mostrar/Esconder</button>
<Texto v-if="showComponent" />

<div v-if="items.length === 0">Carregando...</div>
<ul v-else>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
</template>

0 comments on commit 678644b

Please sign in to comment.