Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 690
Yes Man Avatar asked Oct 24 '25 11:10

Yes Man


1 Answers

Vue 3

This worked for me:

import { useRoute, useRouter } from 'vue-router'

// put this inside your setup() function:

const route = useRoute()
const router = useRouter()

if (route.query.username = 'test') {
  // do stuff...
  
  // clear the query
  router.replace({ query: {} })      
}
like image 59
nth-child Avatar answered Oct 27 '25 02:10

nth-child



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!