Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load child route by default in angular 2

Tags:

angular

routes

At the start of application i want to load child route.

Right now URLcome but respective component not load on that section but when again hit the actual URL it comes.

like route configure is

  const appRoutes: Routes = [         { path: 'a', component: AComponent,          children:[                    { path:'x',component:AXcomponent }                    ]         },         { path: 'b', component: bComponent, } ] 

Now i want to load path a/x how i will load at the start of page ?

like image 862
Rituraj ratan Avatar asked Jan 18 '17 10:01

Rituraj ratan


People also ask

What is default routing in Angular?

Default is "/" (the root path). The path-matching strategy, one of 'prefix' or 'full'. Default is 'prefix'. By default, the router checks URL elements from the left to see if the URL matches a given path and stops when there is a config match.

What is child route in Angular?

With child routes, you can have a component-like structure defined for the routes in your app. It is critical as there are views that the user should not be able to access unless they are in a particular view. This way, the structure becomes tree-like, just like the structure of components.

What is the correct way to add a basic route in Angular?

First, add links to the two components. Assign the anchor tag that you want to add the route to the routerLink attribute. Set the value of the attribute to the component to show when a user clicks on each link. Next, update your component template to include <router-outlet> .


1 Answers

Add empty path routes as redirect automatically

const appRoutes: Routes = [     {         path:'',         redirectTo: 'a',         pathMatch: 'full'      },     {         path: 'a',         component: AComponent,         children:[             {                 path:'',                 redirectTo: 'x',                 pathMatch: 'full'              },             {                 path:'x',                 component: AXcomponent              }         ]     },     {          path: 'b',         component: bComponent     } ]; 
like image 104
Santanu Biswas Avatar answered Oct 07 '22 02:10

Santanu Biswas