简体   繁体   中英

Vue router hooks in NUXT middleware

I am trying to close down the sidebar when my user changes route

export default function({ store }) {
    store.commit("TOGGLE_SIDEBAR");
}

The issue is this calls it as soon as the site loads

I try this

export default function({ app, store }) {
  app.router.beforeEach((to, next) => {
    store.commit("TOGGLE_SIDEBAR");
    next();
  });
}

I get next is not a function.

How do I get this working?

As stated in the documentation router.beforeEach(...) expects a function with 3 arguments : to , from and next .

As you've passed only two arguments, the next argument you are trying to call is in fact the from Route.

Add a third parameter like below :

export default function({ app, store }) {
  app.router.beforeEach((to, from, next) => {
    store.commit("TOGGLE_SIDEBAR");
    next();
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM