36

I'm trying to write my first Vuejs app. I'm using vue-cli and simple-webpack boilerplate.

When I add vue-router links to my app component I get this error in console

Error in render function: "TypeError: Cannot read property 'matched' of undefined"

Here is my code:

App.vue

<template>
  <div>
    <h2>Links</h2>
    <ul>
      <router-link to='/'>Home</router-link>
      <router-link to='/query'>Query</router-link>

      <router-view></router-view>
    </ul>
  </div>
</template>

<script>
    export default {}
</script>

main.js

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
import routes from './routes.js'
import App from './App.vue'

const app = new Vue({
  el: '#app',
  routes,
  render: h => h(App)
})

routes.js

import VueRouter from 'vue-router';
let routes=[
  {
    path: '/',
    component: require('./Components/Home.vue')
  },
  {
    path: '/query',
    component: require('./Components/Query.vue')
  }
];

export default new VueRouter({routes});

7 Answers 7

100

The name when you add it to Vue must be router.

import router from './routes.js'

const app = new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

If, for whatever reason, you want to call the variable routes you could assign it this way.

import routes from './routes.js'

const app = new Vue({
  el: '#app',
  router: routes,
  render: h => h(App)
})
Sign up to request clarification or add additional context in comments.

2 Comments

I would give you 10 stars if I could. I was on a tutorial and it didn't say that we must use router as the variable name. I was using Router.
Yeh the official documentation getting started guide does not work, this answer does.
2

On my Vue file I had the following code:

Then, I modified my app.js file, and place the following code:

import router from './Router/router.js'

const app = new Vue({
    el: '#app',
    router
});

1 Comment

When adding an answer, please try to explain what is actually causing the issue rather than just posting a possible fix where the user can guess what was actually causing it. The accepted answer already explains the actual cause of the issue with the related code on how to fix the issue. Your answer did not add any value to the question in my opinion.
2

vue & vue router & match bug & solution

match bugs

image

image

solution

name must be router

https://stackoverflow.com/a/44618867/5934465

image

OK

image

image


import default module bug

import default module no need {}!

Comments

0

Adding to this, if you are putting the routes in the same page instead of importing it, It must be declared before the Vue component render.

Like this:-

const router = new VueRouter({
  mode: 'history',
  routes:[
    { path: '/dashboard', component: Dashboard},
    { path: '/signin', component: Signin}
  ]
});

new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

Not like this :

new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

const router = new VueRouter({
  mode: 'history',
  routes:[
    { path: '/dashboard', component: Dashboard},
    { path: '/signin', component: Signin}
  ]
});

Comments

0

Just to add my typo that caused this. I forgot the {} on the import

import { router } from './routes.js'  //correct
import router from './routes.js'   //causes same error

Comments

0

If you put some customized codes in router/index.js, make sure you still export default router at the end.

const router = new Router({
  mode: 'history'
});
export default router;

Comments

0

I have faced with this issue same as you. Then I realized at the end of the index.js that I forgot to add :

export default router

Then it solves the issue in my case.

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.