3

Learning vuejs3 I created new @vue/cli app with command

vue create myapp

with vuejs 3 selected I added Router to my project and added Router reference in my src/main.js :

import { createApp } from 'vue'

import { createRouter/*, createWebHistory */ } from 'vue-router'
import WelcomeScreen from './pages/WelcomeScreen.vue'
import UsersList from './pages/UsersList.vue'

import App from './App.vue'

const router = createRouter({
  // history: createWebHistory(),
  mode: 'history',
  routes: [
    { path: '/', component: WelcomeScreen },
    { path: '/users', component: UsersList }
  ]
})
const app = createApp(App)
app.use(router)

app.mount('#app')

But In the console I see warning :

"export 'createRouter' was not found in 'vue-router'

and error :

main.js?56d7:10 Uncaught TypeError: Object(...) is not a function

In my package.json I have :

  "dependencies": {
    "core-js": "^3.6.5",
    "mitt": "^2.1.0",
    "vue": "^3.0.0",
    "vue-router": "^3.4.8"
  },

and

$ vue --version
@vue/cli 4.5.8

Which way is valid? Also are some some vuejs3 tutorials for @vue/cli? I found some vuejs 3 tutorials, but not for @vue/cli and that raise some confusion...

Say in my @vue/cli with vue2 I use file src/router/index.js, but I do not remember if I have created manually...

Thanks!

2

2 Answers 2

13

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Hi.i think you should check this blog again. vuemastery.com/blog/vue-router-a-tutorial-for-vue-3
0

I had the same problem and found the answer here

// uninstall vue2.x vue-router

npm uninstall vue-router --save

// install vue3 vue-router

npm install vue-router@next --save

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.