N
Nuxt3mo ago
Cry0nicS

Pass URL query params to href

I have a button with a link that looks like this
<Button
:href="$t('cta.button.url')"
size="xl"
variant="primary">
{{ $t("cta.button") }}
</Button>
<Button
:href="$t('cta.button.url')"
size="xl"
variant="primary">
{{ $t("cta.button") }}
</Button>
I want to read all the query params from this page and pass them along to this href. I tried something like
<script lang="ts">
const {t} = useI18n();
const route = useRoute();
const query = route.query;

const buttonUrl = computed(() => {
// Get the base URL from translations
const baseUrl = t('cta.button.url');
// Convert the route query parameters to a query string
const queryString = new URLSearchParams(query).toString();

// Return the full URL, appending the query string if it exists
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
});
</script>
<script lang="ts">
const {t} = useI18n();
const route = useRoute();
const query = route.query;

const buttonUrl = computed(() => {
// Get the base URL from translations
const baseUrl = t('cta.button.url');
// Convert the route query parameters to a query string
const queryString = new URLSearchParams(query).toString();

// Return the full URL, appending the query string if it exists
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
});
</script>
However, the URLSearchParams doesn't take a LocationQuery (that is being return from route.query). What's the best way to sort this out?
5 Replies
manniL
manniL3mo ago
you could use ufo's utils instead of URLSearchParams.toString()
manniL
manniL3mo ago
GitHub
GitHub - unjs/ufo: 🔗 URL utils for humans
🔗 URL utils for humans. Contribute to unjs/ufo development by creating an account on GitHub.
Cry0nicS
Cry0nicS3mo ago
Thanks. If I can, however, I'd like to avoid using another library just for this use case Actually, seems like I don't need to install it.
// Stringfy and encode the query object into a query string.
const queryString = stringifyQuery(query);

// Return the full URL, appending the query string if it exists
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
// Stringfy and encode the query object into a query string.
const queryString = stringifyQuery(query);

// Return the full URL, appending the query string if it exists
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
This seemed to have worked.
manniL
manniL3mo ago
that's already part of nuxt 😉
Cry0nicS
Cry0nicS3mo ago
Pretty cool, didn't know. Thanks
Want results from more Discord servers?
Add your server
More Posts
ERR_PNPM_EEXIST on vercel `pnpm install` after nuxt upgrade to 3.11.1Hi, I'm getting ``` ERR_PNPM_EEXIST  EEXIST: file already exists, rename '/vercel/path0/node_modulesError.vue not working with createError?I think I am confused about how to have a custom error page for showing 404 messages. I have a setup[nuxt ui] theming: which config?### edit: while working on other things, somehow the changes took over. They are working in tailwinduseFetch informs the the method is not available for the routeFollowing this thread https://discord.com/channels/473401852243869706/473406506579263488/12222652820useFetch and queries will not re-fetch with watch array of reactive values```ts const route = useRoute(); const page = ref<number>(0); const category = ref<string>(route.queIs there a way to log asset (*.js or *.css file) requests?I added a global server request middleware, but it doesn't seem to capture thesePlease help! Nuxt server/functions - VercelPlease help! Nuxt server/functions - vercel. I upgraded to a pro account and added vercel.json at roCustom key names while building cache key or excluding query paramsDuring caching, we need to exclude some query parameters simply because of the overall cache size. IERROR [h3] [unhandled] Parse Error: Header overflowAfter adding i18n to my project, an error message "[h3] [unhandled] Parse Error: Header overflow" wiUsing $fetch inside app folder to get the the list of routesCurrently, I'm using this approach to get the list of the routes https://nuxt.com/docs/guide/going-f