search

Home  >  Q&A  >  body text

Vue router deletes query parameters

<p>I'm using Nuxt2/Vue2 and I'm having trouble getting the query parameters when generating the website. </p> <p>I created my URL in the nuxt configuration by using an entry in Contentful like this: </p> <pre class="brush:php;toolbar:false;">generate: { crawler: false, async routes() { const items = ( await fetchEntriesByContentType({ contentType: "items" }) ).map(entry => { const routes = { route: `/items/item/${entry.fields.slug}`, payload: entry, } return routes; }); return items; } }</pre> <p>If I console log and try to debug in a Nuxt configuration file, it returns a url with parameters as expected, such as /items/item/101?type=book</p> <p>However, if I try to access it in my Vue component using this.$route.path, this.$route.fullPath or this.$route.query etc., it never returns after the "?" Any content. For example. For the example above it would always be /items/item/101</p> <p>Do I need to add something to the nuxt configuration to include query parameters? I noticed that if I click this.$route to go to the definition, it goes to the vue-router file in my node_modules even though I don't have vue-router installed. Is this causing the problem? </p>
P粉947296325P粉947296325457 days ago716

reply all(1)I'll reply

  • P粉978551081

    P粉9785510812023-09-03 13:18:42

    You will not return anything in the map callback, assuming routes must be returned, it should look like this:

    const items = (
        await fetchEntriesByContentType({ contentType: "items" })
    ).map(entry => {
        const routes = {
            route: `/items/item/${entry.fields.slug}`,
            payload: entry,
        }
        return routes;
    });

    reply
    0
  • Cancelreply