Vue: Vue Router is not navigating to any routes

Viewed 847

I am a newbie to Vue Js, and created a Vue project with @vue/cli 4.5.11.

import { createApp } from 'vue';
import VueRouter from 'vue-router';
import App from './App.vue';
import TeamsList from './components/teams/TeamsList.vue'
import UsersList from './components/users/UsersList.vue'

const router = new VueRouter({
    mode: 'history',
    routes: [
        { path:'/teams',component:TeamsList},
        { path:'/users',component:UsersList}
    ]
});

const app = createApp(App);

app.use(router);

app.mount('#app');

But my app is not navigating to respective routes, and it's appending /# for every route.

3 Answers

Try changing the way you import your Vue Components, you can do the following:

component: () => import('./components/teams/TeamsList')

Here's an example inside the routes array:

routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/teams',
    name: 'Teams',
    component: () => import('./components/teams/TeamsList')
  }
]

Also, try to always add a default '/' path for unrecognized routes.

Here is the solution

vue-router version 4 or newer is working for Vue3.
"vue-router": "^3.5.3" is working for Vue2.

Related