Martin-
Martin-

Reputation: 927

How to setup child routes

I'm trying to figure out how child routes in VueJS work. I would think that if I had a news overview with links to each news item, I could then use a child route to view the news item, but it doesn't works as I expects.

Is it me that is doing it wrong or?

const router = new VueRouter({
    routes: [
    {
      path: '/news',
      name: 'news',
      component: News,
      children: [
        {
          path: ':id',
          name: 'newsitem',
          component: Newsitem
        }
      ]
    }
  ]
});

I have created a jsfiddle to show how I would expect it to work.

If I uncomment the router in the javascript, then it works fine, but not with children.

Upvotes: 6

Views: 48641

Answers (3)

Saintilien Wilson
Saintilien Wilson

Reputation: 51

const News = {
  template: `<div>
<h1>News</h1>
<br><br>
<router-view></router-view>
</div>
`
}
const Newsitem = {
  template: "<h2>News {{this.$route.params.id}}</h2>"
}

const router = new VueRouter({
  routes: [{
    path: '/news',
    name: 'news',
    component: News,
    children: [{
      path: ':id',
      name: 'newsitem',
      component: Newsitem,
    }]
  }]
});


new Vue({
  el: '#app',
  router,
}).mount('#app');
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

<div id="app">
  <ul>
    <li>
      <router-link :to="{ name: 'news'}">News list</router-link>
    </li>
    <li>
      <router-link :to="{ name: 'newsitem', params: { id: 'item-1' }}">Item 1</router-link>
    </li>
    <li>
      <router-link :to="{ name: 'newsitem', params: { id: 'item-2' }}">Item 2</router-link>
    </li>
  </ul>
  <router-view></router-view>
</div>

Upvotes: 0

Moersing.Lin
Moersing.Lin

Reputation: 41

I got it, A Vue Router is required <router-view></router-view>,but in your code, The root component is there, but you forgot the parent,it needs a <router-view></router-view> too.

https://jsfiddle.net/v28yw3g5/

Upvotes: 4

Sebastian
Sebastian

Reputation: 1001

Like Moersing.Lin said you forgot to put a <router-view> in your News Component.

Here is an working example of your fiddle:

const News = {
  template: `<div>
<h1>News</h1>
<br><br>
<router-view></router-view>
</div>
`
}
const Newsitem = {
  template: "<h2>News {{this.$route.params.id}}</h2>"
}

const router = new VueRouter({
  routes: [{
    path: '/news',
    name: 'news',
    component: News,
    children: [{
      path: ':id',
      name: 'newsitem',
      component: Newsitem,
    }]
  }]
});


new Vue({
  el: '#app',
  router,
}).mount('#app');
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

<div id="app">
  <ul>
    <li>
      <router-link :to="{ name: 'news'}">News list</router-link>
    </li>
    <li>
      <router-link :to="{ name: 'newsitem', params: { id: 'item-1' }}">Item 1</router-link>
    </li>
    <li>
      <router-link :to="{ name: 'newsitem', params: { id: 'item-2' }}">Item 2</router-link>
    </li>
  </ul>
  <router-view></router-view>
</div>

Upvotes: 11

Related Questions