Why won't my 'routerLink' items navigation work?
It's a new project based on the only changes:
ng new -S --routing home2
<div style="cursor: pointer;"><a routerLink="/page1" routerLinkActive>Page 1</a></div>
<div style="cursor: pointer;"><a routerLink="/page2" routerLinkActive>Page 2</a></div>
<router-outlet></router-outlet>
import { Routes } from '@angular/router';
import { Page1Component } from './components/page1/page1.component';
import { Page2Component } from './components/page2/page2.component';
export const routes: Routes = [
{path: 'page1', component: Page1Component},
{path: 'page2', component: Page2Component},
];
After running ng new -S --routing home2, angular just prepares an infrastructure for using router. You need then to add:
RouterLink and RouterLinkActive (or just RouterModule instead of three directives):@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, RouterLink, RouterLinkActive],
templateUrl: './app.component.html',
styleUrl: './app.component.less'
})
after adding missing imports your routes should work
Also, you should pass one or several CSS classes to routerLinkActive directive.
For your future questions I would recommend to reproduce your issue using Angular project on STACKBLITZ, this way it will be much easier for others to help you. GOOD LUCK with NG17 :)
You need to add RouterModule in app.component.ts, it is a module provided by Angular for routing functionality, like the example bellow
import { Component } from '@angular/core';
import { RouterModule, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, RouterModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'main-frontend';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With