Home > Net >  Routing navigate method not redirecting to component
Routing navigate method not redirecting to component

Time:10-18

I have been trying to find a solution to my problem since yesterday. Unfortunately, I can't do it. The routing does not work, even when in the url I change by the path. It still takes the HTML from app.component.html

My app.module.ts :

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from './material.module';

import { HomeComponent } from './home/home.component';
import { RouterModule, Routes } from '@angular/router';
import { FormsModule } from '@angular/forms';

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

And my AppRoutingModule :

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  {path: '', component: AppComponent},
  {path: 'home', component: HomeComponent}
];

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

I just want when I put the url: localhost:4200/home that it redirects me to the HomeComponent html. The code is simple and I have followed several tutorials. I hope to have some help because I do not understand .. Thanks to you!

CodePudding user response:

Make sure <router-outlet></router-outlet> is somewhere in your html template as your component will be rendered there.

  • Related