NUXT中间件中的Vue路由器挂钩

时间:2019-05-27 15:59:20

标签: vuejs2 middleware nuxt.js

我试图在用户更改路线时关闭边栏

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

问题是网站加载后立即调用

我尝试

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

我得到next is not a function.

如何使它正常工作?

1 个答案:

答案 0 :(得分:1)

documentation router.beforeEach(...)中所述,期望一个具有3个参数的函数:tofromnext

由于仅传递了两个参数,所以实际上您尝试调用的next参数是from路由。

添加第三个参数,如下所示:

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