Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: NG04002: Cannot match any routes. URL Segment: 'student'

My routing app-routing.module.ts

import { Component, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes ,RouterModule} from '@angular/router';
import { StudentComponent } from './student/student.component';
import { StudentdetailComponent } from './studentdetail/studentdetail.component';

const routes:Routes=[
{
  path:'student',component:StudentComponent
},
{
  path:'studentdetail',component:StudentdetailComponent
}
];

@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ]
})
export class AppRoutingModule { }

student.component.html

<p>student works!</p>

studentdetail.component.html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<p>studentdetail works!</p>

test.component.html

 <a [routerLink] ="['/student']">Student</a><br/>
<a [routerLink] ="['/studentdetail']">Student Details</a><br/>

<div>
<router-outlet></router-outlet>
</div>

appmodule.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import {Test} from './test.component';
import { TopComponent } from './top/top.component';
import { AppRoutingModule } from './app-routing.module';
import { StudentComponent } from './student/student.component';
import { StudentdetailComponent } from './studentdetail/studentdetail.component';
import { RouterModule } from '@angular/router';
@NgModule({
  declarations: [
    AppComponent,
    Test,
    TopComponent,
    StudentComponent,
    StudentdetailComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    RouterModule.forRoot([])
  ],
  providers: [],
  bootstrap: [Test]
})
export class AppModule { }

When I click on the link student and Student Details. I am getting below error after doing F12

Uncaught (in promise): Error: NG04002: Cannot match any routes. URL Segment: ‘student’
Error: NG04002: Cannot match any routes. URL Segment: ‘student’

>Solution :

In your AppModule.ts you have to remove this line : RouterModule.forRoot([])

Then go to your app-routing.module.ts and put this line in the imports/exports :

imports: [
    CommonModule,
    RouterModule.forRoot(routes)
  ],
  exports: [RouterModule]

Then it should work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading