23

I try to make pagination in Nuxt Js project, so how can I change only page query params in my current route, and keep other query params with there values I have 3 query params :

      this.count = this.$route.query.count ?? 8
      this.page = this.$route.query.page ?? 1
      this.sort_by = this.$route.query.sort_by ?? 'latest'
  <li class="pagination-link" v-for="(page, key) in computed.lastPage" :key="key"
        :class="{ 'active': page === computed.currentPage }">
      <nuxt-link :to="'?page='+page" append
                 :class="{'current-link' : computed.currentPage === page}">
        {{ page }}
      </nuxt-link>
    </li>
AndrewShmig
  • 4,843
  • 6
  • 39
  • 68
Anass Ez-zouaine
  • 411
  • 1
  • 6
  • 12

4 Answers4

45

Your best friend - destructuring assignment. JavaScript docs.

Here is an example of how to use it in your case:

let query = this.$route.query;
let newPage = 100;
this.$route.query = {...query, page: newPage};

Without any additional variables it will look much cleaner:

this.$route.query = {...this.$route.query, page: 100};

After code execution you will have query with overwritten page parameter to 100 and the remaining untouched parameters.

PS. Updated on 19.11.2019

As mentioned in comments, $route object is read-only. Change to $router.replace or $router.push:

this.$router.replace({name: path_name, query: {...this.$route.query, page: 100}})
AndrewShmig
  • 4,843
  • 6
  • 39
  • 68
  • 7
    The route object is read-only according to the docs, so how does this not throw a type error? – db2 Nov 04 '19 at 14:31
  • 4
    `TypeError: Cannot assign to read only property 'params' of object '#'` It is definitely throwing a type error. – jhadenfeldt Nov 19 '19 at 00:40
  • 1
    `$router.replace` worked for me. I was was able to remove all query params with `$router.replace({ query: {} })` – justin Jul 13 '20 at 20:14
  • 2
    for me, using Nuxt, ```this.$router.replace``` results in ```NavigationDuplicated: Avoided redundant navigation to current location``` error. But ```this.$router.push({ query: { ...this.$route.query, foo: 'bar' } })``` works without errors. – Vlad Macovei Jun 23 '21 at 19:56
  • there is any other option without error in console por navigaton duplicated? – Alberto Acuña Dec 28 '22 at 10:02
4

Try the following

this.$router.push({path: this.$route.path, query: { ...this.$route.query, foo: 'bar' }})
Hossein Bajan
  • 2,644
  • 5
  • 19
  • 36
0

Yeap, this is a tricky situation w the Vue router. I've posted the answer here https://stackoverflow.com/a/68822330/8904981 for a similar question

Sachin S
  • 186
  • 8
0

In Nuxt.js (will be similar in vue) :

this.$router.replace(`${this.$route.path}?page=${this.page}`)

this.$router.replace will replace the current path with the given string without pushing the change to the history link

this.route.path will query the current path without any queryString (use this.$route.fullPath for the route with query strings)

FlyingZipper
  • 701
  • 6
  • 26