What steps can be taken to resolve the Uncaught TypeError issue with vue-router?

I am currently in the process of upgrading my Vue 2 project to Vue 3. However, I keep encountering errors consistently. The latest error message displayed on my console is as follows:

vue-router.mjs:3499 Uncaught TypeError: Cannot read properties of undefined (reading 'location')

The line causing the issue is number 3499 which contains the code snippet below:

push(routerHistory.location).catch(err => {
                    if ((process.env.NODE_ENV !== 'production'))
                        warn('Unexpected error when starting the router:', err);
                });

This particular file is located within the node_modules directory at (front/node_modules/vue-router/dist/vue-router.mjs) Do you have any suggestions on how to resolve this?

The version of "vue-router" being used is: "^4.1.2",

In my main.js file:

import { library } from '@fortawesome/fontawesome-svg-core';
import {faSpinner} from '@fortawesome/free-solid-svg-icons';
import SortedTablePlugin from 'vue-sorted-table/src';
import { FontAwesomeIcon, FontAwesomeLayers } from '@fortawesome/vue-fontawesome';
import { BootstrapVue3 } from 'bootstrap-vue-3';
import { createApp, h } from 'vue';
import App from './App.vue';
import './registerServiceWorker';
import router from './router';

library.add(
  faSpinner,
);

const app = createApp({
  render: () => h(App),
});

app.use(router);
app.use(BootstrapVue3);
app.use(SortedTablePlugin);
app.component('font-awesome-icon', FontAwesomeIcon);
app.component('font-awesome-layers', FontAwesomeLayers);
app.mount('#app');

In the router/index.js file:

// import Vue from 'vue';
import { createRouter } from 'vue-router';

import FormStep from '@/components/FormStep.vue';
import DualTable from '@/components/DualTable.vue';
import ListTable from '@/components/ListTable.vue';
import CaptureScreen from '@/components/CaptureScreen.vue';
import StageScreen from '@/components/StageScreen.vue';
import TopScreenConfig from '@/components/TopScreenConfig.vue';
import EmailCapture from '@/components/EmailCapture.vue';
import store from '@/store';

// Vue.use(VueRouter);

const routes = [
  ...
];

const router = createRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.matched.some((record) => record.meta.requiresAuth) && !store.state.user.isAuthenticated) {
    next('/login/');
    return;
  }
  next();
});

export default router;

Could it possibly be related to Vue.use(VueRouter); ?

Answer №2

To create a router instance in Vue JS, follow these steps:

import { createRouter,createWebHistory } from 'vue-router';
....
const router = createRouter({
  history: createWebHistory(),
  base: process.env.BASE_URL,
  routes,
});

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

What causes the removeChild function to exhibit distinct behaviors when it comes to list items versus i tags?

It has come to my attention that the removeChild function does not behave in the same way with all elements, particularly when dealing with list items. I am currently using the i tag for icons from frontAwesome and would like to individually remove these i ...

Obtaining a result from a Promise in AngularJS

Here is a snippet of an Angular JS Service that I have: 'use strict'; app.factory('loggedService', ['$http', 'authService', 'customerService', function ($http, authService, customerService) { var out = ...

"Mastering the art of utilizing Async in combination with waterfall and Recursion in node

I've developed a script for transferring data from Dynamo to a MySQL database. Initially, I encountered performance issues on the SQL side due to not using asynchronous calls. To address this, I integrated the async library to throttle the Dynamo segm ...

Is there a limit to the number of else if statements I can use? The final else statement

How can I enhance this code snippet? The last else if statement is not working, despite my efforts to fix it. var selectedDntTyp = $("#donationTypDD"); if (selectedDntTyp.length > 0) var DropInitValue = selectedDntTyp.val(); if(DropInitValue == &apos ...

Erasing a comment creates empty spaces

I've encountered an issue with my idea whiteboard where removing comments leaves unwanted blank spaces between ideas. For example, if I have the following comments: But when I delete something: Is there a way to ensure that no gaps are left between ...

The largest allowable call stack size within jQuery version 1.9.0

I have a question about poorly written code that I've been experimenting with. It's messy, but it's just for fun. I was attempting to create a "like system" where a user clicks on a button and the object ID associated with the button is sent ...

Issue with calling hooks. Hooks can only be invoked within the body of a function component

Looking to develop a todo application using React, but encountering an issue with the useContext hook. The error message "Invalid hook call..." suggests a few possible reasons for this problem: Possible mismatched versions of React and the renderer (e.g ...

Ways to update a component when the value of a Promise is altered

I am struggling with Vue component re-rendering due to a problem related to consuming data from a Promise. The data is fetched and stored under the specific property chain (visualData.layout.cube...), where I assign values to DATA properties (such as label ...

Tips for storing and replicating jQuery events

I am working on saving jQuery events in a database. // This Function is called On Click function trackevent(event){ window.events.push(event) } $.each(window.events, function(i, item){ console.log(i +" - "+ $.parseJSON(item)); }); The events a ...

Dealing with an Incorrect Date in JavaScript

After working on a JavaScript logic to extract date and time from certain values, I realized that my approach needed some adjustments. Initially, I parsed the DateTime and converted it to a string. Then, I split the string to retrieve the date component. ...

Improve code efficiency by streamlining a function and using more effective syntax techniques

I've been learning how to condense code with jQuery. Can this script be written in a more concise manner without everything being on one long line? items.push('<li id="' + key + '">' + ' (key: ' + key + ')&apo ...

What is the best method for inserting the HTML content from a specific div into a textarea?

Everything seems to be working fine, but once I insert the HTML into the textarea, an issue arises where the div gets wrapped within another div, causing the layout to break. var urls = []; $('body').on('click', '.btn_video&apos ...

Creating a custom JavaScript library using an existing npm module (codius)

Embarking on a new journey with this, never tried it before. Currently utilizing https://github.com/codius/codius-host. The development of Codiu§ has been abandoned, however I am determined to salvage some parts of it for my own project. It is crucial fo ...

Having trouble with your JavaScript regex not functioning properly?

I am currently working with an array of arrays and I need to iterate through it to retrieve each word, excluding any "@", punctuation, and hashtags. However, my regular expression seems to be removing certain words entirely from the array and I can't ...

Extensive content within the v-autocomplete field

I am trying to use the v-autocomplete component to display the entire text of the selected item. On initial selection, everything displays correctly: However, once it is chosen, only a portion of the text is shown, leaving out important information: ...

Flatbuffers does not exist in this context

Currently, I am working on a nodeJs application that involves the use of Google Flat Buffer. After installing flatc on my MacBook Pro, I compiled the schema below: namespace MyAlcoholist; table Drink { drink_type_name: string; drink_company_name: stri ...

Issue with the string formatting in Vue.js is causing unexpected behavior

Hi there, I'm currently working on dynamically styling a div in VueJs and running into an issue with the this.currentProgressLevel not being applied to the width property within the currentStyle object. I've attached a screenshot of the code I&ap ...

Updating Angular view based on service parameter change

In-depth Inquiry I have a specific setup with a header view and a main view in my Angular application. The goal is to include a "back" button in the header that should only be visible based on the current page I'm viewing. Actions Taken In my app ...

Enhance the overall appearance of select elements across all components using Material UI's

I'm attempting to change the border color of my outlined select elements using the code below, but it's not working as expected. It seems that the border style is being inherited from the fieldset element. I've experimented with using MuiOut ...

What is the best way to display information using axios.get?

I am currently working on a small project with PHP and Axios. I am attempting to display data in an HTML table using the v-for directive, but I am facing issues as I receive server feedback. Although I am able to retrieve the JSON response successfully, wh ...