Angular 17 Standalone Navbar Component
Here's how to implement a standalone navbar component in Angular 17:
1. First, create the navbar component
ng generate component navbar --standalone
2. Here's the complete implementation:
navbar.component.ts
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-navbar',
standalone: true,
imports: [CommonModule, RouterModule],
templateUrl: './navbar.component.html',
styleUrl: './navbar.component.css'
})
export class NavbarComponent {
isMenuOpen = false;
navItems = [
{ path: '/', label: 'Home' },
{ path: '/about', label: 'About' },
{ path: '/services', label: 'Services' },
{ path: '/contact', label: 'Contact' }
];
toggleMenu() {
this.isMenuOpen = !this.isMenuOpen;
}
}
navbar.component.html
<nav class="navbar">
<div class="navbar-container">
<a routerLink="/" class="navbar-brand">MyApp</a>
<!-- Mobile menu button -->
<button class="navbar-toggler" (click)="toggleMenu()" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">☰</span>
</button>
<!-- Desktop navigation -->
<div class="navbar-collapse" [class.show]="isMenuOpen">
<ul class="navbar-nav">
@for (item of navItems; track item.path) {
<li class="nav-item">
<a
routerLink="{{item.path}}"
routerLinkActive="active"
[routerLinkActiveOptions]="{exact: true}"
class="nav-link"
>
{{item.label}}
</a>
</li>
}
</ul>
</div>
</div>
</nav>
navbar.component.css
.navbar {
background-color: #333;
padding: 1rem;
color: white;
}
.navbar-container {
display: flex;
justify-content: space-between;
align-items: center;
max-width: 1200px;
margin: 0 auto;
}
.navbar-brand {
color: white;
text-decoration: none;
font-size: 1.5rem;
font-weight: bold;
}
.navbar-toggler {
background: none;
border: none;
color: white;
font-size: 1.5rem;
cursor: pointer;
display: none;
}
.navbar-nav {
display: flex;
list-style: none;
margin: 0;
padding: 0;
}
.nav-item {
margin-left: 1rem;
}
.nav-link {
color: white;
text-decoration: none;
padding: 0.5rem 1rem;
border-radius: 4px;
transition: background-color 0.3s;
}
.nav-link:hover {
background-color: #555;
}
.nav-link.active {
background-color: #4CAF50;
}
@media (max-width: 768px) {
.navbar-toggler {
display: block;
}
.navbar-collapse {
display: none;
width: 100%;
margin-top: 1rem;
}
.navbar-collapse.show {
display: block;
}
.navbar-nav {
flex-direction: column;
}
.nav-item {
margin: 0.5rem 0;
}
}
3. Using the Navbar Component
To use this navbar in your application, you can simply add it to your layout component or app component:
import { Component } from '@angular/core';
import { NavbarComponent } from './navbar/navbar.component';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [NavbarComponent, RouterOutlet],
template: `
<app-navbar />
<main>
<router-outlet />
</main>
`,
})
export class AppComponent {}
Key Features of this Implementation:
- Standalone Component: Doesn't require an NgModule
- Responsive Design: Works on both mobile and desktop
- Router Integration: Uses Angular's RouterModule for navigation
- Active Route Highlighting: Shows which route is currently active
- Mobile Menu Toggle: Collapsible menu for smaller screens
- Type Safety: Uses TypeScript for better development experience
You can customize the navigation items, styling, and behavior to match your application's requirements.