首页 > 解决方案 > NUXT 中间件中的 Vue 路由器挂钩

问题描述

当我的用户更改路线时,我正在尝试关闭侧边栏

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.

我如何让这个工作?

标签: vuejs2middlewarenuxt.js

解决方案


文档 router.beforeEach(...)中所述,需要一个具有 3 个参数的函数tofromnext.

由于您只传递了两个参数,因此next您尝试调用的参数实际上是fromRoute。

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

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

推荐阅读