Vue-router error: TypeError: Cannot read property 'matched' of undefined

后端 未结 5 2062
醉酒成梦
醉酒成梦 2020-12-28 15:45

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

相关标签:
5条回答
  • 2020-12-28 16:08

    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)
    })
    
    0 讨论(0)
  • 2020-12-28 16:09

    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
    });
    
    0 讨论(0)
  • 2020-12-28 16:09

    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
    
    0 讨论(0)
  • 2020-12-28 16:15

    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 {}!

    0 讨论(0)
  • 2020-12-28 16:30

    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}
      ]
    });
    
    0 讨论(0)
提交回复
热议问题