1
votes

When I run angular router, I get the 404 status - page not found

I've been trying to follow the @angular docs to get the routing for a test site setup.

It is partially working, because I setup a redirect in the app-routing.module.ts file, and it successfully redirects from localhost:4200 to localhost:4200/test.

However, once at that path I get a 404 error. It should be displaying the component I routed to that path.

Code

I have one component called anomalies

Project Structure

leaving out the files I believe to be non-relevant to this problem (I built the project with ng commands, so the rest should be as expected)

app/
  anomalies/
    anomalies.component.css
    anomalies.component.html
    anomalies.component.spec.ts
    anomalies.component.ts
  app-routing.module.ts
  app.module.ts

app-routing.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { AnomaliesComponent } from './anomalies/anomalies.component';


const routes: Routes = [
  {
    path: 'test',
    component: AnomaliesComponent
  },
  {
    path: '',
    redirectTo: 'test',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes)
  ],
  exports: [
    RouterModule
  ]
})

export class AppRoutingModule {}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { AnomaliesComponent } from './anomalies/anomalies.component';
import { AppRoutingModule } from './app-routing.module';


@NgModule({
  declarations: [
    AppComponent,
    AnomaliesComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

anomalies/anomalies.component.html

<p>
  anomalies works!

  well, not yet.
</p>
2
I couldn't understand what issue you are facing.. You said it successfully redirects. Even though it redirects, the html is not loading and it throws this error?Leandro Lima
yep, that's correctAlter
Dude, I literally copied and pasted your code and it worked.. Try to stop application and serve it again. Or it's a kinda issue that I've never seen before. github.com/garapa/studying/tree/master/testRoutingLeandro Lima
I've stopped and started it, but had the same issue... maybe you're right and this is a new issue. Going to keep trying for a bit longerAlter
Can you put a piece of code into stackblitz.com/edit/angular-grc3nx .Dakota

2 Answers

2
votes

Defing two things in case its missed :

  1. index.html should have a <base-href='/'>

  2. You should have router-outlet defined in app.component.html <router-outlet></router-outlet>

0
votes

the <router-outlet> tag was required in app.component.html

Code

app.component.html

<router-outlet></router-outlet>