Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Router - named router-outlet navigation from code

Using @angular/router": "3.4.7".

Proposed solution here doesn't work anymore.

      /**
        The ProductComponent depending on the url displays one of two info 
         components rendered in a named outlet called 'productOutlet'.
       */
        @Component({
          selector: 'product',
          template: 
          ` <router-outlet></router-outlet>
            <router-outlet name="productOutlet"></router-outlet>
          `
        })
        export class ProductComponent{
         }
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild([
          {
            path: 'product',
            component: ProductComponent,
            children: [
              {
                path: '',
                component: ProductOverviewComponent,
                outlet: 'productOutlet'},
              {
                path: 'details',
                component: ProductDetailsComponent,
                outlet: 'productOutlet' }
            ]
          }
      ]

    )],
  declarations: [
    ProductComponent,
    ProductHeaderComponent,
    ProductOverviewComponent,
    ProductDetailsComponent
  exports: [
    ProductComponent,
    ProductHeaderComponent,
    ProductOverviewComponent,
    ProductDetailsComponent
  ]
})
export class ProductModule {

}

Manual navigation works as expected

http://localhost:5000/app/build/#/product-module/product (correctly displays overview component in named outlet)

http://localhost:5000/app/build/#/product-module/product/(productOutlet:details)

(correctly displays details component in named outlet)

THE PROBLEM

Cannot figure out the correct way to perform programatical navigation:

this.router.navigateByUrl('/(productOutlet:details)');

this.router.navigate(['', { outlets: { productOutlet: 'details' } }]);

Following errors occur:

Error: Cannot match any routes. URL Segment: 'details'.

like image 987
Mandark Avatar asked Jun 13 '17 09:06

Mandark


People also ask

Can we use 2 router outlet in Angular?

You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc.

What is named router outlet in Angular?

What is named router outlet? It is a feature designed for applications with multiple router outlets. A single router outlet is usually enough for most modern websites with clean and intuitive designs.

Can you identify the use of router outlet directive from the following options?

The router-outlet is a directive that's available from the @angular/router package and is used by the router to mark where in a template, a matched component should be inserted. Thanks to the router outlet, your app will have multiple views/pages and the app template acts like a shell of your application.


1 Answers

You can navigate programatically like this

this.router.navigate([{ outlets: { outletName: ['navigatingPath'] } }], { skipLocationChange: true });

Note: skipLocationChange is use to hide the url from the address bar.

Refer the official document : https://angular.io/guide/router

like image 107
senthil Avatar answered Oct 04 '22 16:10

senthil