1

I've been researching this all morning and can't seem to figure this out. I keep getting this error but followed (what I thought) the fix from others. I'm new to routing within a nav component. I'm sure you're not surprised!

Any help is appreciated.

Thanks!

main-nav.component.ts

import { Component, inject } from '@angular/core';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { AsyncPipe } from '@angular/common';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatButtonModule } from '@angular/material/button';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatListModule } from '@angular/material/list';
import { MatIconModule } from '@angular/material/icon';
import { Observable } from 'rxjs';
import { map, shareReplay } from 'rxjs/operators';
import { RouterOutlet, RouterLink } from '@angular/router';


@Component({
  selector: 'app-main-nav',
  standalone: true,
  templateUrl: './main-nav.component.html',
  styleUrl: './main-nav.component.scss',
  imports: [
    MatToolbarModule,
    MatButtonModule,
    MatSidenavModule,
    MatListModule,
    MatIconModule,
    AsyncPipe,
    RouterOutlet,
    RouterLink
  ]
})
export class MainNavComponent {
  private breakpointObserver = inject(BreakpointObserver);

  isHandset$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset)
    .pipe(
      map(result => result.matches),
      shareReplay()
    );
}

main-nav.component.html

<mat-sidenav-container class="sidenav-container">
    <mat-sidenav #drawer class="sidenav" fixedInViewport
        [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
        [mode]="(isHandset$ | async) ? 'over' : 'side'"
        [opened]="(isHandset$ | async) === false">
      <mat-toolbar>Menu</mat-toolbar>
      <mat-nav-list>
        <a mat-list-item routerLink="/company-create">Create Company</a>
        <a mat-list-item href="#">Link 2</a>
        <a mat-list-item href="#">Link 3</a>
      </mat-nav-list>
    </mat-sidenav>
    <mat-sidenav-content>
      <mat-toolbar color="primary">
        @if (isHandset$ | async) {
          <button
            type="button"
            aria-label="Toggle sidenav"
            mat-icon-button
            (click)="drawer.toggle()">
            <mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
          </button>
        }
        <span>Company LLC</span>
      </mat-toolbar>
      <div class="content-area">
        <router-outlet name="content"></router-outlet>
      </div>
  </mat-sidenav-content>
</mat-sidenav-container>

app.routes.ts

import { Routes } from '@angular/router';
import { CompanyCreateComponent } from './components/company-create/company-create.component';
import { MainNavComponent } from './main-nav/main-nav.component';

export const routes: Routes = [
  {
    path: '',
    component: MainNavComponent,
    children: [
      { path: 'company-create', component: CompanyCreateComponent, outlet: 'content' },
      { path: '', redirectTo: 'company-create', pathMatch: 'full', outlet: 'content' }, // Default view           
    ]
  }
]
3
  • 1
    Try to change your router link to this: [routerLink]="[{ outlets: { content: ['company-create'] } }]" Commented May 17 at 11:48
  • @MilosStojanovic This did not work Commented May 17 at 19:12
  • So you do not have a main route at all for 'company-create' ? you are using an outlet for that ? Commented May 19 at 6:02

1 Answer 1

1

Your auxiliary route in [routerLink] should be as below so that it will use the named router outlet instead of default router outlet (without name attribute).

<a
  mat-list-item
  [routerLink]="[{ outlets: { content: ['company-create'] } }]"
>
  Create Company
</a>

Assume that you have the (primary) <router-outlet> in your starting component.

Demo @ StackBlitz

Reference: Auxiliary Routes in Angular

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

5 Comments

I still can't get this to work. I am using Angular 19. Would I put the content from the app.routing.module file in the app.routes file in an Angular 19 project? I am getting this error: Module '"./app.routes"' declares 'routes' locally, but it is not exported.
You should import with import {routes} from './app.routes'; in your app.routing.module.ts. If you are only export routes in app.route.ts, you can revamp to: const routes: Routes = [...]; export default routes;. Checkout my demo link for app.route.ts and app-routing.module.ts.
Thanks again. It fixed my error but now I can't seem to get my component to load lol. Lots of learning here.
Perhaps, can you create a demo in StackBlitz to reproduce the issue so that I can help to check it. Thanks.
My routes file was a little off. I updated it and this got it to work. Thank you! @Yong Shun

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.