I am learning Angular JS using TypeScript. I have experience in Angular JS. But when it is integrated with TypeScript, it is completely new to me. I am now configuring the route for my application.
This is my app.module.ts file
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { DirectoryComponent } from './directory/directory.component';
//import { AppRoutingModule } from './app-routing.module';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path : 'directory',
component : DirectoryComponent
}
];
@NgModule({
declarations: [
AppComponent,
HomeComponent,
DirectoryComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(routes)
],
providers: [ ],
bootstrap: [AppComponent]
})
export class AppModule { }
I configured route in that file. When I access the routes directly from the browser, it is working. Then I tried to add links in the app.component.html. This is my app.component.html
<h1>
{{title}}
</h1>
<div>
<ul>
<li>
<a [routerLink]="['/']">Home</a>
</li>
<li>
<a [routerLink]="['directory']">Directory</a>
</li>
</ul>
</div>
<div>
<router-outlet></router-outlet>
</div>
Then I click on the links. Both are not working at all. But when I access directly entering route values in the URL bar, it was working. My HomeComponent and DirectoryComponent are simple. They are just showing a message.
It is working when I first access to the ‘/directory’ url. It shows directory page. From there when I cllick home link, it is updated to home page. But the url is still ‘directory’. But when I click the directory link again, nothing happens.
I followed these solutions
routerLink is not working in angular 2
routerlink not working in angular2
All are not working.
I inspected the HTML elements, it is rendering the link properly. The problem is when I click on the link, nothing happens.
I am using the latest version of the Angular JS. What is wrong with my code?