Search code examples
angularangular2-routingangular-routing

Navigate to child route using named router outlet


I have ngtabcontent so its content should be dynamic so create child routes for the PersonalComponent and Now on click of next it should navigate to its child route.

    const routes: { path: '', redirectTo: 'tenant', pathMatch: '{ path: "personal", component:  children: [
      { path: 'employer', component: AddemployerComponent, outlet: 'test'},
    ]
  },
  { path: "applicant", component: AddapplicantComponent },
  { path: 'tenant', component: TenantappComponent },
  { path: 'animal', component: AddanimalComponent },
  { path: 'vehicle', component: AddvehiclesComponent },
  { path: 'background-info', component: BackgroundInfoComponent },
  { path: 'termsandconditions', component: TermsandconditionsComponent },
  { path: 'payment', component: PaymentComponent }
];

So this is my routing module where Intially I display my personal component and after next click its should navigate the child route employer which has a name for the outlet.

nextFn() {
if (this.router.url === '/personal') {
  this.router.navigate(['employer', {outlets: 'test'}]);
  this.shared.isSubmitPayment = true;
} else if (this.router.url === '/employer') {
  this.router.navigate(['animal']);
} else if (this.router.url === '/animal') {
  this.router.navigate(['vehicle']);
} else if (this.router.url === '/vehicle') {
  this.router.navigate(['background-info']);
} else if (this.router.url === '/background-info') {
  this.router.navigate(['termsandconditions']);
} else if (this.router.url === '/termsandconditions') {
  this.router.navigate(['payment']);
}}

In component file my route goes like this..

    <div class="row personal-info">
  <div class="input-group">
    <div class="input-group-prepend">
      <div class="input-group-text" id="btnGroupAddon"><i class="fa fa-check" aria-hidden="true"></i></div>
    </div>
    <p class="registration"><b>Registration Successful</b></p>
  </div>
</div>
<app-tenant-header></app-tenant-header>
<div class="form-feilds">
  <h6>Personal Information</h6>
  <ngb-tabset>
    <ngb-tab [id]="tab.id" *ngFor="let tab of tabs;let i = index;">
      <ng-template ngbTabTitle>
        {{tab.title}}{{applicantNumberArray[i - 1]}}
      </ng-template>
      <ng-template ngbTabContent>
          <div *ngIf="!shared.isSubmitPayment">
              <app-formfields [personalInfo]=personalInfo [tabIndex]=i [tabs]=tabs (addApplication)="addApplicantFn($event)" (deleteApplication)="deleteApplicantFn($event)"></app-formfields>
          </div>
          <div *ngIf="shared.isSubmitPayment">
            <router-outlet></router-outlet>
          </div>
      </ng-template>
    </ngb-tab>
  </ngb-tabset>
</div>
<div class="col-md-12 col-sm-12 col-xs-12 property-footer">
  <app-footer></app-footer>
</div>

and my html goes like this ....

Thanks in Advance


Solution

  • There is a difference between a child route and a named route.

    A child route is for routes that should appear within another template. They are defined as you have it, with the children property in the route configuration. A child route is routed to a router outlet in the parent component's template.

    A named route is for routes that should appear as a sibling to another template, such as a side by side display. They are specified with a router outlet defined with a name.

    Guessing from your example, you want a child route not a named route. So you should take off the outlet property from your code.

    Remove it here:

    { path: 'employer', component: AddemployerComponent, outlet: 'test'}
    

    And here:

    ['employer', {outlets: 'test'}]