vuejs2vue-router

How to remove all query parameters from current route in vue router?


All my attempts to remove the query string fails:

// Initial state
this.$router.replace({
    name: 'routeName',
    query: {
        param: 123
    }
});

// Errors
this.$router.replace({ query: {}});
this.$router.replace({ query: undefined });
this.$router.replace({ query: null });

How to remove query without any errors?

I'm using Vue Router v3.1.5


Solution

  • For some reason router.replace() & router.push() need a non-empty object as query. So all rest to do is clean your initial query object from values, like:

    let query = {
      param: [1, 2, 3]
    };
    
    // Initial state
    this.$router.push({
      name: 'yourRouteName',
      query: query
    });
    
    // clean your initial query object
    query.param = [];
    
    // Now replace it
    this.$router.replace({
      query: query
    });