Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to a child route by route name?

I know we can go to a route by its name using $router.push({ name: 'route-name' }).

What I want to know is how to do that with a child route name. This is my route structure:

export default [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/Home.vue'),
    childs: [
      {
        name: 'home.about',
        path: '/about',
        component: import('@/views/Home/About.vue')
      }
    ]
  }
]

But my console says [vue-router] Route with name 'home.about' does not exist for $router.push({ name: 'home.about' }).

What I'm missing?

Obs: The idea is to not route to the child using a hard route path.

like image 844
Eleandro Duzentos Avatar asked Mar 13 '19 11:03

Eleandro Duzentos


People also ask

How do you navigate to a parent route from a child route?

As an example, if you have a child route edit/:id and your id is an URI formatted like /api/something/123 then in order to get to your parent using a relative route you need to use [../../../..'] .

Where are child URL routes defined?

Just like we had a <router-outlet></router-outlet> for the root application component, we would have a router outlet inside the ProductDetails component. The components corresponding to the child routes of product-details would be placed in the router outlet in ProductDetails .

What is a child route?

A child route is like any other route, in that it needs both a path and a component . The one difference is that you place child routes in a children array within the parent route.

Which property is used to define child routes within a routes object?

You can create a nested routing by defining child routes using the children property of a route (alongside a path and component properties). You also need to add a nested router-outlet in the HTML template related to the component linked to the parent route (In our case it's the admin route).

How do I define a child route?

All you need to define child routes is to add an array of additional route configuration objects as part of a children key in your parent configuration. Here’s an example configuration: And don’t forget to add a router-outlet directive in the template of any parent component:

How many child routes can a route have in angular?

A route can have one or more than one child routes. Child routes can have their own child routes and so on. A route is created using Angular Route interface and array of Route is Routes type. Route has different properties to handle route such as path, component, outlet, children, loadChildren etc.

How to find the child routes of a product route?

The router then takes the remainder of the URL segment ‘detail/id’ and continues to search for the child routes of Product route. It will match it with the path ‘detail/:id’ and instantiates the ProductDetailComponent and renders it in the <router-outlet> directive present in the ProductComponent

Do I have to have a path to all my routes?

You don’t have to necessarily have paths to all your routes. Let’s take this variation on the previous configuration for example: Notice how the route with the ChildComponent component doesn’t have a path.


2 Answers

You have a typo.

It should be children and not childs.

export default [
{
  path: '/',
  name: 'home',
  component: () => import('@/views/Home.vue'),
  children: [
   {
    name: 'home.about',
    path: '/about',
    component: import('@/views/Home/About.vue')
   }
  ]
 }
]
like image 155
Radu Diță Avatar answered Nov 14 '22 05:11

Radu Diță


 const router = new VueRouter({
  routes: [{ 
        path: '/foo',
        name: 'foo',
        component: Foo,
        children: [
        {
          path: 'fooChild1',
          name: 'fooChild1',
          component: FooChildComponent
        },
        {
          path: 'fooChild2',
          name: 'fooChild2',
          component: FooChildComponent
        }
      ]
    }, { 
        path: '/bar', 
        component: Bar
    }]
})

Now if you wish to navigate to fooChild1 then use $router.push({ name: 'fooChild1' }) or if you wish to navigate to fooChild2 then use $router.push({ name: 'fooChild2' })

like image 36
Hardik Avatar answered Nov 14 '22 05:11

Hardik