Once Global Route Guards are implemented in Vue Router, the URL stops updating

After diligently following the VueMastery courses, I encountered an unexpected issue that has me stumped.

Interestingly, when I do not use the Global Router Guards, the URL updates as expected in both modes. However, once I add the specified hooks to my router (router/index.js), there are no errors thrown but the URL fails to update:

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

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

Details of my setup include:

  • @vue/cli 4.2.3
  • vue-router 3.1.5

The full script within my router file (router/index.js) is as follows:

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
  },
  // Additional paths...
];

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;

This router configuration is then imported and utilized in my main.js file:

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

// Auto Component Registration

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

// More code...

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

Despite trying hash mode as well, the URLs refuse to update upon clicking router links. What could be causing this behavior?

Answer №1

There would typically be an error warning you about this particular issue

TypeError: next is not a function

This occurs because the variable next has not been defined within the afterEach function, as it marks the end of the routing middleware chain. To resolve this, simply remove the reference to next within afterEach, and the functionality should resume:

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

For more information, refer to this documentation

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transforming cURL commands into jQuery AJAX

Attempting to perform an API call using jQuery AJAX with curl configuration provided by the backend team. curl -X POST \ https://example1.com/api/sms \ -H 'Accept: application/json' \ -H 'Cache-Control: no-cache' & ...

Sharing information between External JavaScript and React JS/Redux

Incorporating React-redux into an externalJs application (built on a custom JS framework) has posed a challenge for me. I am trying to initialize data for the Redux store from externalJS, but it seems that the external script is unable to access the React ...

Trying out an ajax request in React by clicking a button

I have been working on testing a simple Login component that involves filling out an email and password, then clicking a button to log in. When the login button is clicked, it triggers an ajax post request using axios. I am interested in testing both happy ...

Request made to Ajax fetching complete webpage content

I have a page full of random tips at http://www.javaexperience.com/tips To display these tips on other pages of the website, I am using an ajax call to add the content returned by the call to a specific div's HTML. The code for the div is: <div ...

Can a shape similar to an inverted circle be created using CSS and jQuery?

I'm stumped on how to create an inverted circle in the corners. I've included a picture for reference. https://i.stack.imgur.com/jeNdh.jpg Is it feasible to achieve this effect using CSS3 or maybe jQuery? ...

How to Access ViewBag Value from Asp.Net in AngularJs Controller

Just getting started with AngularJs and encountering an issue with passing a value from a .Net MVC View to an AngularJs Controller. Here's the code snippet in question: AngularJs controller snippet: app.controller("RatingApiController", function ($s ...

Troubleshooting Problem with Bootstrap 4 Navigation Drop-Down Menu

I am working on developing some navigation forms for my university projects. I encountered an issue where I selected the Book item, and the navigation was working fine. However, when I selected Child items and then clicked on the Organization item, the nav ...

Interfacing Node JS with Java Web Services via SOAP

I've been attempting to connect Java web services from a Node.js module, but I'm encountering an error in the wsdl library. Below is my wsdl file: <!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130 ...

Utilizing AJAX for showcasing the data from an XML document

Our professor gave a brief explanation of AJAX, expecting us to showcase the data from an XML file in a scrollable text area on our website. Unfortunately, I am facing issues with loading the XML file into the designated div area. Any assistance or suggest ...

The importance of dependencies in functions and testing with Jasmine

In troubleshooting my AngularJS Service and Jasmine test, I encountered an issue. I am using service dependency within another service, and when attempting to perform a Unit Test, an error is thrown: TypeError: undefined is not an object (evaluating sso ...

Duplicating a dropdown menu using a list of checkboxes (jQuery)

Is there a way to convert a fieldset containing a legend and a list of checkboxes/radio buttons into a select HTML element? It might sound like an unusual request, but unfortunately, it's something I need to accomplish. I have created a jsfiddle for ...

Shopping Dialog with Bootstrap (nakupanda) captures form input [JSFiddle]

Having difficulty extracting data from my form. Currently utilizing the bootstrap dialog from nakupanda () The dialog (located within a fullcalendar select function) var form = $('#createEventForm').html(); BootstrapDialog.show({ mes ...

React Redux is functioning flawlessly when paired with Sample A code, but unfortunately it is encountering a dispatch error when used with

Having an issue with React Redux: Sample A Code works perfectly, but encountering dispatch error with Sample B Code I am working on displaying Post records and then sending them back to the server backend via an onclick function. In order to achieve this ...

Why isn't my onClick event functioning as expected?

I used the handleClick function in an onClick event, but it's showing an error (this is not defined). var Buttons = React.createClass({ getInitialState() { return { field: ':P ' } }, handleClick(field ...

Tips on including a trash can symbol to rows in a bootstrap table using React

I am working on a table that contains various fields, and I want to add a trash icon to each row so that when it is clicked, the specific row is deleted. However, I am encountering an issue where the trash icon is not showing up on the row and I am unable ...

Tips for choosing a week on a calendar with JavaScript

Here is the code for my calendar: <script> $(document).ready(function() { var events = <?php echo json_encode($data) ?>; $('#calendar').fullCalendar({ header: { left: 'prev,next', center: &apos ...

Is there a way to connect a Button to a different page in VUE.JS?

I currently have a button that needs to be linked to another page. This is the code I am working with at the moment. How can we write this login functionality in vue.js? It should direct users to the page "/shop/customer/login" <div class="space-x ...

Accessing the parent element from a clicked child element within a nested v-for loop

Is there a way to access the parent element of a clicked child element using the @click method? For example: <div v-for="(item, index) in bubles"> {{item.name}} <div v-for="subItem in item.bubles"> <a @click="openModal(subItem)"&g ...

Ways to modify the styles of two unique IDs located within separate classes

Let me share what I'm searching for. I've set up a carousel that displays 7 SVG icons, each identified by an id. To customize the appearance of the current icon, I'm using the following CSS: .slick-current #icon-1 path { fill: rgb(252, ...

Instantly refreshing the Angular DOM following data modifications and retrieval from the database

I am currently working on a Single Page Application that uses Angular 8 for the frontend and Laravel for the backend. This application is a CRUD (Create, Read, Update, Delete) system. The delete functionality is working as expected, deleting users with spe ...