Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom querystring parser #1259

Closed
trikadin opened this issue Mar 16, 2017 · 13 comments
Closed

Custom querystring parser #1259

trikadin opened this issue Mar 16, 2017 · 13 comments

Comments

@trikadin
Copy link

Please, add the opportunity to use custom qs lib instead of built-in.

@posva
Copy link
Member

posva commented May 16, 2017

This was shipped in 2.4.0 https://router.vuejs.org/en/api/options.html#parsequery--stringifyquery

@posva posva closed this as completed May 16, 2017
@ganigeorgiev
Copy link

ganigeorgiev commented Jun 3, 2017

It is not very clear from the documentation what the parameters and their types are. From the source, I suppose that parseQuery receives a String and stringifyQuery receives an Object.

// https://github.com/ljharb/qs
import qs from 'qs';

const router = new Router({
    routes: [
        // ...
    ],
    // set custom query resolver
    parseQuery(query) {
        return qs.parse(query);
    },
    stringifyQuery(query) {
        var result = qs.stringify(query);

        return result ? ('?' + result) : '';
    }
});

@rayrutjes
Copy link

I think we should allow to override the query parsing methods on a per route basis.
When you are trying to use a third party component that has special requirements regarding query parsing, you don't want to change the way existing URLs are parsed.
Does that make sense @posva , is it maybe already possible?

@posva
Copy link
Member

posva commented Aug 9, 2017

@rayrutjes The url being global, I really think that's a bad idea to change it based on the current component. Appart from that I don't think it's even doable

@rayrutjes
Copy link

I think you are right @posva, per route doesn't make sense.
Then, I would militate to have a better url parsing by default that handles nested objects & co.
qs works perfectly ;) but we would like to not have existing users to have to change the way urls are parsed for our library.

For now, we are going to make sure that we are compatible with what is supported.

@posva
Copy link
Member

posva commented Aug 9, 2017

I proposed that already some time ago but vue-router is 8.1kb gziped and qs is 2.6gziped.
Most people don't need nesting in queries, so being able to remove 20% of the size for most people is definitely a good thing

@rayrutjes
Copy link

Yes I agree, sadly.
We will work on a solution on our side ;)

@christhofer
Copy link

christhofer commented Sep 12, 2019

@posva can you add ganigeorgiev's comment above to the documentation?

Current documentation doesn't show how to use ths option.
image

@omerciftcibasi
Copy link

Addition to ganigeorgiev's comment, the query parameters inside routes will be automatically converted url string and parsed as an object when accessing $router.query

@tomisicm
Copy link

tomisicm commented Aug 8, 2020

Does parseQuery / stringifyQuery work with the vue-router 4.0 and TS?
There are plenty of TS errors.

@avxkim
Copy link

avxkim commented Mar 26, 2021

If someone faced an issue overriding parseQuery or stringifyQuery in nuxt, here's solution:

//in nuxt.config.js
  router: {
    parseQuery(q) {
      return require('qs').parse(q)
    },
    stringifyQuery(q) {
      const r = require('qs').stringify(q)
      return r ? '?' + r : ''
    },
  },

There was an error about undefined qs variable, when i tried to import it at the top of the nuxt.config file.

@9mm
Copy link

9mm commented Apr 25, 2023

Does anyone know why this is getting ignored? It's not even calling those functions

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  parseQuery(query) {
    console.log('y');
    return qs.parse(query);
  },
  stringifyQuery(query) {
    console.log('x');
    const result = qs.stringify(query, { encodeValuesOnly: true });
    return '?xxx';
  },
  routes: [
    {
      path: '/',
      name: 'root',
      component: () => import('../layouts/LayoutApp.vue'),
      children: [

@AlejandroAkbal
Copy link

AlejandroAkbal commented Jun 1, 2023

In Nuxt 3 you can use this:

  1. Create app/router.options.ts

  2. Paste this:

import type { RouterConfig } from '@nuxt/schema'
import qs from 'qs'

// https://router.vuejs.org/api/interfaces/routeroptions.html
export default <RouterConfig>{
  // Fix router query parser - https://github.com/vuejs/vue-router/issues/1259#issuecomment-1571553624
  parseQuery: qs.parse,

  stringifyQuery: qs.stringify
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests