Kay
Kay

Reputation: 19660

Angular - How to navigate to a named router outlet in a .ts file

I have a side nav and within the side nav i have named router outlet. I am trying to assign the "aside" named router outlet to a sub component called "top-words-aside". However, it cannot find the url segment.

reports.component.html

<mat-sidenav #rightsidenav position="end" fixedInViewport="fixed">
    <router-outlet name="aside"></router-outlet>
</mat-sidenav>

reports.routing.module.ts

const routes: Routes = [
    {
        path: '',
        component: ReportsComponent
    },
    {
        path: ':id',
        component: ReportDetailComponent,
        resolve: {
            job: JobResolver,
            report: ReportResolver,
            activity: ActivityResolver,
        },
        children: [
            {path: 'top-words-aside', component: TopWordsAsideComponent, outlet: 'aside'},
        ]
    }
];

reports.component.ts

open() {
    this.rightSideNavService.open();
    this.router.navigate([{ outlets: { aside: ['top-words-aside'] }}]);
}

However i get the following error.

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'top-words-aside' Error: Cannot match any routes. URL Segment: 'top-words-aside'

Upvotes: 4

Views: 5453

Answers (1)

shangzhouwan
shangzhouwan

Reputation: 246

do you import the router in your module?

you can define

private route: ActivatedRoute

in constructor funtion.

In the open function

this.router.navigate([{ outlets: { aside: ['top-words-aside'] }}],{relativeTo:this.route});

Upvotes: 2

Related Questions