4

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

RouterLink does not work

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.

enter image description here

I am using the latest version of the Angular JS. What is wrong with my code?

zgue
  • 3,793
  • 9
  • 34
  • 39
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • Which message did you get? – Swanand Taware Dec 26 '17 at 03:49
  • I got no 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 happen. What is the possible error please? What is the issue? – Wai Yan Hein Dec 26 '17 at 12:27
  • remove square bracket from '[routerLink] and [directory] amd ['/'] – Swanand Taware Dec 27 '17 at 06:15

3 Answers3

0

it's because of home directory change routerlink to another name it will fix your problem.

instead of '' use some names

Vinay Kumar
  • 386
  • 2
  • 8
0

I was having the same issue, and it turns out that the rout will do nothing if the route does not exist. I had

<nav>
    <a routerLink="/review" >Full reviews</a>
</nav>

But my rout was set to review (no s)
Hope this helps anyone else

imstupidpleasehelp
  • 1,618
  • 2
  • 16
  • 36
0

For me it was that I did not import the component in declarations in app.module.ts

@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    ReactiveFormsModule,
    HttpClientModule,
    AppRoutingModule,
  ],
  declarations: [
    //make sure your components are declared here
    ResetPasswordConfirmedComponent,
  ],

After that make sure that you import router into the component and it should work!

For reference, I am using Angular 15.