Skip to content

how to trigger vue-query from emit event? #5783

Answered by Mini-ghost
h1jun asked this question in Q&A
Discussion options

You must be logged in to vote

useQuery can only be called at the top level of setup function.

You can try this:

// [Layout] - Default.vue
<script setup lang="ts">
import Main from "../components/Main.vue";
import { useQuery } from "@tanstack/vue-query";
import { ref } from 'vue'

const fetchUsers = async () => {
  const res = await fetch("https://jsonplaceholder.typicode.com/users");
  return res.json();
};

const enabled = ref(false)
const { data } = useQuery(["users"], fetchUsers, {
  enabled,
})

const clickAPiCall = () => {
  enabled.value = true
};

watch(data, (value) => {
  console.log(value)
})
</script>

<template>
  <Main @clickAPiCall="clickAPiCall" />
</template>

<style lang=""></style>

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@h1jun
Comment options

Answer selected by h1jun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants