Skip to content

useFetch query params #1637

Answered by curtgrimes
extrem7 asked this question in Q&A
May 25, 2022 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

I think using a computed like this is probably one good way to pass reactive query parameters in for a GET request. In this demo you can change the input value and see that new URL gets fetched immediately:

Demo: https://stackblitz.com/edit/vue3-script-setup-with-vite-ppshjm?file=src%2FApp.vue

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useFetch, useUrlSearchParams } from '@vueuse/core';

const foo = ref('12345');

const url = computed(() => {
  const _url = new URL('https://httpbin.org/get');
  _url.search = new URLSearchParams({
    foo: foo.value,
  });
  return _url.toString();
});

const { data, isFetching } = useFetch(url, { refetch: true });
</script>

<t…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@wheatjs
Comment options

@curtgrimes
Comment options

@mskr
Comment options

@mskr
Comment options

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