2

I am planning to start learning angular 2 component router.

I have used Angular ui-router heavily.All my projects uses UI-router complex features like nested states and nested named views.

What will be good start to use angular 2 component router?

how can I configure nested states in Angular 2 component router?

1
  • please do follow the tutorial given in official website of angular 2. you can find Router & Navigation under Advanced topic. Commented Nov 3, 2016 at 10:42

3 Answers 3

4

All in all i would say routing is pretty simple and intuitive in angular 2

I would suggest reading through the router docs to get all the basics.

Keep in mind that child components can have routes too. They build from its parents routes.

app.component.ts (excerpt)

@Component({ ... })
@RouteConfig([
  {path:'/crisis-center/...', name: 'CrisisCenter', component: CrisisListComponent},
  {path:'/heroes',        name: 'Heroes',       component: HeroListComponent},
  {path:'/hero/:id',      name: 'HeroDetail',   component: HeroDetailComponent}
])
export class AppComponent { }

crisis-center.component.ts(excerpt)

@RouteConfig([
  {path:'/',         name: 'CrisisCenter', component: CrisisListComponent, useAsDefault: true},
  {path:'/:id',      name: 'CrisisDetail', component: CrisisDetailComponent}
])

Notice that the path ends with a slash and three trailing periods (/...).

That means this is an incomplete route (AKA a non-terminal route). The finished route will be some combination of the parent /crisis-center/ route and a route from the child router that belongs to the designated component.

All is well. The parent route's designated component is the CrisisCenterComponent which is a Routing Component with its own router and routes.

From angular.io router guide

Sign up to request clarification or add additional context in comments.

Comments

1

You can define a app.routing.ts like below.

export const routes: Routes = [
    {
      path: '',
      component: SimpleLayoutComponent,
      data: {
         title: 'Login form'
      },
     children: [
         {
             path: '', component: LoginComponent,
         }
     ]
    },
    {
      path: 'dashboard',
      component: FullLayoutComponent,
      data: {
        title: 'Home'
      },
      children: [
         {
             path: '',
             component: 'mycomponent'
         }
       ]
     }
];

Then import this class to your app.module.ts file like below.

 import { AppRoutingModule }             from './app.routing';
 @NgModule({
 imports: [
    BrowserModule,
    HttpModule,
    FormsModule,
    AppRoutingModule,
 ],
 declarations: [
    AppComponent,
    LoginComponent
 ],
 providers: [
 UserService, AuthGuard],
 bootstrap: [ AppComponent ]
 })
export class AppModule { }

app.component.ts Below: views get injected in

import { Component } from '@angular/core';

@Component({
    selector: 'body',
    template: '<router-outlet></router-outlet>'
 })
export class AppComponent { }

Comments

0
@NgModule({
declarations: [AppComponent,CreateComponent,ListComponent],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpModule,
RouterModule.forRoot([
  {path:"",component:ListComponent},
  {path:"Create",component:CreateComponent},
  ])
],
bootstrap: [AppComponent]

})

Put this RouterModule in app.module file.

For this you have to import { RouterModule} ;

<router-outlet></router-outlet>

Put router-outlet element in app.component.html to render component through routing.

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.