Technologies:
Tolerim
15 days ago
In Vue.js router configuration, an error is occurring that states "cannot read properties of undefined (reading 'use')"
Uncaught runtime errors:
ERROR
Cannot read properties of undefined (reading 'use')
TypeError: Cannot read properties of undefined (reading 'use')
at eval (webpack-internal:///./src/router.js:10:45)
at ./src/router.js (http://localhost:8080/js/app.js:79:1)
at webpackrequire_ (http://localhost:8080/js/app.js:203:33)
at fn (http://localhost:8080/js/app.js:436:21)
at eval (webpack-internal:///./src/main.js:4:65)
at ./src/main.js (http://localhost:8080/js/app.js:69:1)
at webpackrequire_ (http://localhost:8080/js/app.js:203:33)
at http://localhost:8080/js/app.js:1324:109
at webpackrequire_.O (http://localhost:8080/js/app.js:249:23)
at http://localhost:8080/js/app.js:1325:53
import Vue from "vue";
import VueRouter from "vue-router";
import Scoreboard from "./components/ScoreboardTable.vue";
import Submissions from "./components/SubmissionDetails.vue";
Vue.use(VueRouter);
const routes = [
{ path: "/scoreboard", component: Scoreboard },
{ path: "/submissions/:problemId", component: Submissions },
];
const router = new VueRouter({
routes,
});
export default router;
Answers(1)
Tolerim
15 days ago
Verified Answer
The error message suggests that there is an issue with the router configuration in the src/router.js file. Specifically, the error is being caused by attempting to read the use property of an undefined object.
Upon inspecting the code provided, it appears that the VueRouter library is not being imported correctly. The following line is attempting to import VueRouter:
import VueRouter from "vue-router";
However, the error message suggests that VueRouter is undefined, which indicates that the import was unsuccessful. This issue can be resolved by installing and importing vue-router correctly.
To install vue-router using npm, you can run the following command in your terminal:
Then, update the import statement in your src/router.js file to the following:
import { createRouter, createWebHistory } from "vue-router";
This import statement is using the createRouter and createWebHistory functions from vue-router. Replace the router initialization code with the following:
const router = createRouter({
history: createWebHistory(),
routes,
});
export default router;
This should resolve the issue and allow the router to function properly.