TypeError: Cannot read property ‘location’ of undefined in Vue router

By | March 14, 2021

If you are getting the above error, then the chances are that you have included createWebHistory in your vue router definition. You probably have:

import { createApp } from 'vue'
import { createRouter } from 'vue-router'
import App from './App.vue'

import Home from './components/Home'

const router = createRouter({
    routes: [
        {path:'/', component: Home}
    ]
})

const app = createApp(App)
app.use(router)

app.mount('#app')

When you should have

import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'

import Home from './components/Home'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {path:'/', component: Home}
    ]
})

Leave a Reply

Your email address will not be published.