My routing in the angular2 apps works well. But I am going to make some routeLink based on this:
Here is my routing:
const routes: RouterConfig = [
{ path:'home' , component: FormComponent },
{ path:'about', component: AboutComponent },
{ path:'**' , component: FormComponent }
];
And here are the links that I made:
<ul class="nav navbar-nav item">
<li>
<a routerLink='/home' routerLinkActive="active">Home</a>
</li>
<li>
<a routerLink='/about' routerLinkActive="active">About this</a>
</li>
</ul>
I expect that, when I click on them it navigates to the corresponding component, but they do not perform anything?
[routerLink]='[/home']
? What Angular2 version and router version are you using?
[routerLink]="['/home']"
directives: [ROUTER_DIRECTIVES],
to your component's metadata. Without that, Angular won't know to parse the routerLink
s.
The code you are showing there is absolutely correct.
I suspect that your problem is that you are not importing RouterModule (which is where RouterLink is declared) into the module which uses this template.
I had a similar problem and it took me some time to solve as this step is not mentioned in the documentation.
So go to the module that declares the component with this template and add:
import { RouterModule } from '@angular/router';
then add it to your modules imports e.g.
@NgModule({
imports: [
CommonModule,
RouterModule
],
declarations: [MyTemplatesComponent]
})
export class MyTemplatesModule { }
Along with having the correct import statements, you'll also need a place for that routerLink to be shown, which is in the <router-outlet></router-outlet>
element, so that also needs to be placed somewhere in your HTML markup so the router knows where to display that data.
don't forget this to add this below in your template:
<router-outlet></router-outlet>
Try changing the links as below:
<ul class="nav navbar-nav item">
<li>
<a [routerLink]="['/home']" routerLinkActive="active">Home</a>
</li>
<li>
<a [routerLink]="['/about']" routerLinkActive="active">About this</a>
</li>
</ul>
Also, add the following in the header of index.html:
<base href="/">
use it like this for mroe info read this topic
<a [routerLink]="['/about']">About this</a>
routerLink
should work ;-)
"@angular/router": "~3.4.0"
. Cheers!
For anyone having this error after spliting modules check your routes, the following happened to me:
public-routing.module.ts:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: '**', redirectTo: 'home' } // ← This was my mistake
{ path: 'home', component: HomeComponent },
{ path: 'privacy-policy', component: PrivacyPolicyComponent },
{ path: 'credits', component: CreditsComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'news', component: NewsComponent },
{ path: 'presentation', component: PresentationComponent }
]
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class PublicRoutingModule { }
app-routing.module.ts:
const routes: Routes = [
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Move { path: '**', redirectTo: 'home' }
to your AppRoutingModule:
public-routing.module.ts:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'home', component: HomeComponent },
{ path: 'privacy-policy', component: PrivacyPolicyComponent },
{ path: 'credits', component: CreditsComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'news', component: NewsComponent },
{ path: 'presentation', component: PresentationComponent }
]
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class PublicRoutingModule { }
app-routing.module.ts:
const routes: Routes = [
{ path: '**', redirectTo: 'home' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
I'm aware this question is fairly old by now, and you've most likely fixed it by now, but I'd like to post here as reference for anyone that finds this post while troubleshooting this issue is that this sort of thing won't work if your Anchor tags are in the Index.html. It needs to be in one of the components
I was using routerlink instead of routerLink.
The links are wrong, you have to do this:
<ul class="nav navbar-nav item">
<li>
<a [routerLink]="['/home']" routerLinkActive="active">Home</a>
</li>
<li>
<a [routerLink]="['/about']" routerLinkActive="active">About this
</a>
</li>
</ul>
You can read this tutorial
There is also another case which suits this situation. If in your interceptor, you made it return non Boolean value, the end result is like that.
For example, I had tried to return obj && obj[key]
stuff. After debugging for a while, then I realize I have to convert this to Boolean type manually like Boolean(obj && obj[key])
in order to let the clicking pass.
Following the working sample, I have figured out solution for the case of pure component:
Declare component at app level Do not init in component
For not very sharp eyes like mine, I had href
instead of routerLink
, took me a few searches to figure that out #facepalm.
If you have your navbar inside a component and you declared your style active in that stylesheet, it won't work. In my case this was the problem.
my item of my navbar using angular material was:
<div class="nav-item">
<a routerLink="/test" routerLinkActive="active">
<mat-icon>monetization_on</mat-icon>My link
</a>
<mat-divider class="nav-divider" [vertical]="true"></mat-divider>
so I put the style active in my style.scss in the root
a.active {
color: white !important;
mat-icon {
color: white !important;
}
}
I hope it helps you if the other solutions didn't.
In my case I had line-wrapper in my VS code and, apparently, there was no space between end of closing quote belonging to [routerLink] and next attribute. Line-wrapper split this in two lines so missing space went unnoticed.
Most of the time problem is a spelling mistake in
<a [routerLink]="['/home']" routerLinkActive="active">Home</a>
Just check again for spelling.
I was able to solve my problem my moving my navigation links from Index page to component (I actually had added template in index.html)
Success story sharing
HttpErrorComponent
which displays error information. So for instance if my page is not loaded because of Permission guard and just displays error 403 - the home button withrouterLink
on this page does not work (not clickable).