How to use useRoute() in nuxt? #520
Answered
by
HendrikJan
HendrikJan
asked this question in
Q&A
-
I'm stuck trying to get a query parameter from the current url. Normally, I would use Try to use useRoute directly: /* /server/helpers/myHelper.ts */
const route = useRoute(); // <-- error: "useRoute is not defined" Try to import from "#imports" or "#app": /* /server/helpers/myHelper.ts */
import { useRoute } from '#imports'; // <-- error: "'useRoute' is not exported by virtual:#imports"
import { useRoute } from '#app'; // <-- error: "'nuxt app instance unavailable" Or try to import from "vue-router": /* /server/helpers/myHelper.ts */
import { useRoute } from 'vue-router';
// Error:
// Named export 'useRoute' not found.
// The requested module 'node_modules/vue-router/dist/vue-router.common.js' is a CommonJS module And finally trying using common.js syntax: /* /server/helpers/myHelper.ts */
import vueRouter from 'vue-router';
const { useRoute } = vueRouter; // <-- error: Property 'useRoute' does not exist on type 'typeof VueRouter' So my question: how can I get the query parameters of the current url? |
Beta Was this translation helpful? Give feedback.
Answered by
HendrikJan
Sep 7, 2022
Replies: 1 comment
-
I found that you now can use functions from import { getQuery } from 'h3'
const lang = getQuery(event).lang; // <-- get the "lang" parameter from the query
console.log('language is set to:', lang) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
HendrikJan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found that you now can use functions from
unjs/h3
like so: