Component Files
The component is located atsrc/app/components/shared/navbar/:
navbar.ts- Component logicnavbar.html- Template with inline styles
TypeScript Implementation
navbar.ts
Component Configuration
Selector:app-navbar
- Used in templates as
<app-navbar></app-navbar>
[RouterLink, RouterLinkActive]
RouterLink- Directive for navigationRouterLinkActive- Directive for styling active routes
This is a standalone component, meaning it directly imports the directives it needs without requiring an NgModule.
HTML Template
navbar.html
Styling
Navigation Bar Styles
display: flex- Flexbox layoutjustify-content: space-around- Distributes links evenly with space around them- Dark theme with
#212121background
Link Styles
Active link: Red text with underline
The styles are defined inline in the template file using a
<style> tag. These styles are still scoped to the component.Router Directives
RouterLink
Navigates to different routes when clicked:- Attribute binding:
routerLink="/path"- For simple static paths - Property binding:
[routerLink]="['/path']"- For dynamic paths or paths with parameters
RouterLinkActive
Automatically applies a CSS class when the route is active:- Adds the
activeclass when the route matches - Removes the class when navigating away
- Works with the CSS rule
nav a.active
RouterLinkActiveOptions
Controls how route matching works:exact: true- Only match if the route is exactly/exact: false(default) - Match if the route starts with/
Navigation Links
The navbar provides access to all four pages:| Link | Route | Component | Exact Match |
|---|---|---|---|
| Contador | / | CounterPage | Yes |
| Hero | /hero | HeroPageComponent | No |
| Dragonball | /dragonball | DragonballPage | No |
| Dragonball Super | /dragonball-super | DragonballSuperPage | No |
Usage in App Component
The Navbar is used in the main App component:app.ts
app.html
- Navbar at the top
<router-outlet />for page content- Footer at the bottom
The App component imports both
RouterOutlet (for rendering routed components) and Navbar (the navigation component) in its standalone imports array.Router Configuration
The routes are defined inapp.routes.ts and match the navbar links:
Key Patterns
Standalone Component with Directives
Import router directives directly in the component:Exact Matching for Root Route
Always use exact matching for the root route:Inline Styles in Template
You can include<style> tags directly in the template file:
Key Takeaways
- Shared components - Reusable across the application
- RouterLink - Declarative navigation without full page reloads
- RouterLinkActive - Automatic active state management
- Exact matching - Essential for root routes to prevent always-active state
- Standalone imports - Import router directives directly in the component
- Flexbox layout - Modern CSS for responsive navigation
- Visual feedback - Active route styling improves UX