How to add Router to @vue/cli app with vuejs 3?

Solution 1:

You need install vue-router like this for vue3:

npm install vue-router@next --save

Router.js

import { createApp } from 'vue'
import App from './App.vue'
    import { createRouter, createWebHistory } from "vue-router"
    const routeInfos = [
        {
            path : "/",
            component : HomePage
        },
        {
            path : "/favorites",
            component : FavoritesPage
        }
    ]
    
    const router = createRouter({
        history : createWebHistory(),
        routes : routeInfos
    })
    
    export default router;

import In Main.js

import router from "./router"
createApp(App).use(router).mount('#app')