【问题标题】:After adding Global Route Guards in Vue Router the URL won't update anymore在 Vue Router 中添加 Global Route Guards 后,URL 将不再更新
【发布时间】:2020-07-02 00:58:21
【问题描述】:

我一直在学习 VueMastery 课程,偶然发现了一个我自己似乎无法解决的意外问题。

如果不使用 Global Router Guards,URL 会在两种模式下正常更新。但是,只要我将以下钩子添加到我的路由器 (router/index.js),我就不会收到任何错误,但 URL 将不再更新:

router.beforeEach((routeTo, routeFrom, next) => {
  NProgress.start();
  next();
});

router.afterEach((routeTo, routeFrom, next) => {
  NProgress.done();
  next();
});

使用:

  • @vue/cli 4.2.3
  • vue-router 3.1.5

我的完整路由器 (router/index.js) 文件包含以下脚本:

import Vue from "vue";
import VueRouter from "vue-router";
import EventList from "../views/EventList.vue";
import store from "@/store/index";
import NProgress from "nprogress";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "event-list",
    component: EventList
  },
  {
    path: "/event/create",
    name: "event-create",
    component: () =>
      import(/* webpackChunkName: "event-create" */ "../views/EventCreate.vue")
  },
  {
    path: "/event/:id",
    name: "event-show",
    component: () =>
      import(/* webpackChunkName: "event-show" */ "../views/EventShow"),
    props: true,
    beforeEnter(routeTo, routeFrom, next) {
      store.dispatch("event/fetchEvent", routeTo.params.id).then(event => {
        routeTo.params.event = event;
        next();
      });
    }
  }
];

const router = new VueRouter({
  mode: "history",
  routes
});

router.beforeEach((routeTo, routeFrom, next) => {
  NProgress.start();
  next();
});

router.afterEach((routeTo, routeFrom, next) => {
  NProgress.done();
  next();
});

export default router;

并且在我的 main.js 中导入并使用:

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

// Automatic Global Component Registraton
import upperFirst from "lodash/upperFirst";
import camelCase from "lodash/camelCase";

// NProgress
import "nprogress/nprogress.css";

const requireComponent = require.context(
  // The relative path of the components folder
  "./components",
  // Whether or not to look in subfolders
  false,
  // The regular expression used to match base component filenames
  /Base[A-Z]\w+\.(vue|js)$/
);

requireComponent.keys().forEach(fileName => {
  // Get component config
  const componentConfig = requireComponent(fileName);

  // Get PascalCase name of component
  const componentName = upperFirst(
    camelCase(
      // Gets the file name regardless of folder depth
      fileName
        .split("/")
        .pop()
        .replace(/\.\w+$/, "")
    )
  );

  // Register component globally
  Vue.component(
    componentName,
    // Look for the component options on `.default`, which will
    // exist if the component was exported with `export default`,
    // otherwise fall back to module's root.
    componentConfig.default || componentConfig
  );
});

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

什么可能导致 URL 不再更新?我尝试使用默认哈希模式,但发生了同样的问题。单击路由器链接时,URL 不会更新。

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-router


    【解决方案1】:

    应该有一个错误告诉你

    TypeError: next 不是函数

    因为next 没有在afterEach 中定义,因为那是路由中间件链的末端。如果您从afterEach 中删除next,它应该会再次工作:

    router.afterEach((routeTo, routeFrom) => {
      NProgress.done();
    });
    

    这里是a link to the docs

    【讨论】:

    • 完美!事实上,在控制台中是错误的。太感谢了! :D
    猜你喜欢
    • 2019-09-10
    • 2021-01-09
    • 2019-05-19
    • 2021-11-04
    • 2023-03-24
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多