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?
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
RouterLink is a built-in Angular Directive that lets you link to specific routes in your app. In the SPA(single-page application), you change what the user sees by showing or hiding portions of the display that correspond to particular components, rather than going out to the server to get a new page.
routerLinkActive is simply an indicator of whether this link represents the active route. @jessepinho - Tried it - [routerLinkActive]="'active'" will only work if you also have [routerLink] in the a-tag. If you had (click)="navitageTo('/route')" instead of [routerLink], and in that function, you called this.
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.
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