简体   繁体   中英

routerLink is not working in Angular

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.

在此处输入图片说明

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

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

instead of '' use some names

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM