Search code examples
angulartypescriptangular2-routing

Angular adding new Module with routing doesn't work. But already existing Modules work


TL;DR In my case I needed to change the import order of modules in AppComponent. I needed to import my SearchModule before AppRoutingModule.

But a better way to solve it is to use LazyLoading and don't import the page modules in the AppModule. Like the user Pieterjan said.

Im trying to add a new Module with child route to the Spring Pet Clinic Angular.

But when I add a module with routing it doesn't work. I did compare it to all the other modules with routing and I cant find a relevant difference.

What I did to create the Module

  • ng g m search --routing
  • added routerLink in app component
  • ng g c search/search dashboard
  • added search route to search-routing-module.ts
  • added the searchmodule to appmodule

but all I get when I go to the link is the standart error page not found.

to the owner routing module and that worked for some reason.

But I cant get it to work from the newly created searchModule.

new search.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SearchRoutingModule } from './search-routing.module';
import { SearchDashboardComponent } from './search-dashboard/search-dashboard.component';


@NgModule({
  declarations: [
    SearchDashboardComponent
  ],
  imports: [
    CommonModule,
    SearchRoutingModule
  ]
})
export class SearchModule { }

search-routing-module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { SearchDashboardComponent } from './search-dashboard/search-dashboard.component';

const routes: Routes = [
  {path: 'search', component: SearchDashboardComponent}
];

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

app.module.ts

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpClientModule} from '@angular/common/http';
import {AppComponent} from './app.component';
import {AppRoutingModule} from './app-routing.module';
import {OwnersModule} from './owners/owners.module';
import {PetsModule} from './pets/pets.module';
import {VisitsModule} from './visits/visits.module';
import {PetTypesModule} from './pettypes/pettypes.module';
import {VetsModule} from './vets/vets.module';
import {PartsModule} from './parts/parts.module';
import {SpecialtiesModule} from './specialties/specialties.module';
import {HttpErrorHandler} from './error.service';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {SearchModule} from './search/search.module';


@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    OwnersModule,
    PetsModule,
    VisitsModule,
    PetTypesModule,
    VetsModule,
    SpecialtiesModule,
    PartsModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    SearchModule
  ],
  providers: [
    HttpErrorHandler,
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

app.component.html


<router-outlet></router-outlet>


Solution

  • Import your module before AppRoutingModule in app.module.ts

        imports: [
            BrowserModule,
            FormsModule,
            HttpClientModule,
            OwnersModule,
            PetsModule,
            VisitsModule,
            PetTypesModule,
            VetsModule,
            SpecialtiesModule,
            PartsModule,
            SearchModule,
            BrowserAnimationsModule,
            AppRoutingModule,
      ],