Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch on Route changes with Nuxt and asyncData

Hi everybody i'm trying to watch on route changes in my nuxt js app.

Here my middleware:

    export default function ({ route }) {
  return route; but i don't know what to write here
}

index.vue File

  middleware: [routeReact]

i'm trying to write this:

app.context.route = route

but it says to me that app.context doesn't exist

Here's the point of my question i'm trying to update my data that gets from my api with axios on page if route changing like this

this the page enter image description here

i'm clicking link to next page :

enter image description here

but when i'm route to next page, nothing happens all data is the same:

enter image description here

here my asyncData code:

asyncData({ app }) {
return app.$axios.$get('apps/' + app.context.route.fullPath.replace(/\/categories\/?/, ''))
.then(res => {
  return {
    info: res.results,
    nextPage: res.next,
    prevPage: res.prev
    };
  })

}

Thanks for your help

like image 346
Albert Jovinskyi Avatar asked Oct 18 '18 12:10

Albert Jovinskyi


People also ask

When to use asyncData vs fetch Nuxt?

Unlike fetch , asyncData cannot access the component instance ( this ). Instead, it receives the context as its argument. You can use it to fetch some data and Nuxt will automatically shallow merge the returned object with the component data.

How does Nuxt routing work?

Nuxt automatically generates the vue-router configuration for you, based on your provided Vue files inside the pages directory. That means you never have to write a router config again! Nuxt also gives you automatic code-splitting for all your routes.

Can I use Vue 3 with Nuxt 2?

Vue Compatibility By using vue-demi they should be compatible with both Nuxt 2 and 3.

How do you do pagination in Nuxt?

For our pagination component we need to pass in the prop of prevPage and set it to true of false if the page number is greater than 1. We also pass in our nextPage and pageNo props and finally our urlPrefix which gets our category from the route params.


1 Answers

First thing, context.route or it's alias this.$route is immutable object and should not be assigned a value.

Instead, we should use this.$router and it's methods for programmatic navigation or <nuxt-link> and <router-link>.

As I understand, you need to render the same route, but trigger asyncData hook in order to update component's data. Only route query is changed.

Correct way to navigate to the same page but with different data is to use link of such format:

<nuxt-link :to="{ name: 'index', query: { start: 420 }}"

Then you can use nuxt provided option watchQuery on page component and access that query inside asyncData as follows:

watchQuery: true,

asyncData ({ query, app }) {
  const { start } = query
  const queryString = start ? `?start=${start}` : ''
  return app.$axios.$get(`apps/${queryString}`)
    .then(res => {
      return {
        info: res.results,
        nextPage: res.next,
        prevPage: res.prev
      }
    })
},

This option does not require usage of middleware. If you want to stick to using middleware functions, you can add a key to layout or page view that is used. Here is an example of adding a key to default layout:

<nuxt :key="$route.fullPath" />

This will force nuxt to re-render the page, thus calling middlewares and hooks. It is also useful for triggering transitions when switching dynamic routes of the same page component.

like image 51
aBiscuit Avatar answered Oct 17 '22 03:10

aBiscuit