Angular navigation paths directing to incorrect components

For my blog application, I have set up separate routing modules for the admin and user sides. However, I am facing an issue where the client side routes are not being recognized after importing the routing module into app.module.ts. Instead of navigating to the specified components, it is redirecting to the page-not-found component. Below is the content of my app.module.ts file

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

import { AppRoutingModule } from './app-routing.module';
import { ClientModule } from './client/client-routing/client.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './client/home/home.component';
import { FooterComponent } from './client/footer/footer.component';
import { FeaturedBlogComponent } from './client/featured-blog/featured-blog.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';



@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    FooterComponent,
    FeaturedBlogComponent,
    PageNotFoundComponent

  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    ClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './client/home/home.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';

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

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

client.module.ts

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

import { ClientRoutingModule } from './client-routing.module';
import { BlogListComponent } from '../blog-list/blog-list.component';
import { BlogDetailComponent } from '../blog-detail/blog-detail.component';
import { AboutComponent } from '../about/about.component';
import { ContactComponent } from '../contact/contact.component';


@NgModule({
  declarations: [BlogListComponent, BlogDetailComponent, AboutComponent, ContactComponent],
  imports: [
    CommonModule,
    ClientRoutingModule
  ]
})
export class ClientModule { }

client-routing.module.ts

import { Routes, RouterModule } from '@angular/router';
import { BlogListComponent } from '../blog-list/blog-list.component';
import { BlogDetailComponent } from '../blog-detail/blog-detail.component';
import { AboutComponent } from '../about/about.component';
import { ContactComponent } from '../contact/contact.component';


const routes: Routes = [
  {path: 'blogs', component: BlogListComponent},
  {path: 'blog/:_id', component: BlogDetailComponent},
  {path: 'about', component: AboutComponent},
  {path: 'contact', component: ContactComponent}
];

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

Despite setting up all modules correctly, I am unable to access the client side routes such as AboutComponent or BlogListComponent, as they result in displaying the page-not-found component.

Answer №1

When utilizing feature modules like the ClientModule, it's important to note that your client module routes may not be loaded immediately. Only the routes defined in app-routing.module.ts are eagerly loaded by default. To ensure that the ClientModule is lazy loaded, you can make a simple modification in your app-router.module.ts file.

// Update the routes array in app-router.module.ts with this line of code.
{ path: 'blog', loadChildren: () => import('./path/to/client.module').then(m => m.ClientModule) }

Don't forget to update the routes in your client-router.module.ts as well:

const routes: Routes = [
  {
    path: '', // The base route is now 'blog' or any other name you specified in app-routing.module.ts
    component: BlogListComponent,
    children: [ { path: ':_id', component: BlogDetailComponent } ]
  },
  { path: 'about', component: AboutComponent },
  { path: 'contact', component: ContactComponent }
];

Additionally, it might be beneficial to separate About and Contact routes from the ClientModule if that aligns better with your project structure.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular: Unable to load todo list when filtering results

I am currently developing a simple todo application using Angular. I have encountered an issue where filtering results from an array imported from services does not happen in real-time. The filtering only works when I navigate to another page (since it&apo ...

Having trouble with 'npm <script-command>' not working? Try changing it to 'npm run-script <script-command>' instead

Currently, I am configuring a node js backend to operate on TS for the first time within a mono-repo that has a specific folder structure. You can view the structure here. The package.json file is located in the main directory as shown below: "scr ...

Is there a way for me to determine the value that has been assigned to a <li> key attribute in React using Jest and testing-library/react?

In my current project, I am using a combination of React with TypeScript and Jest along with Testing Library for testing purposes. I have a specific requirement to unit test some code where I need to ensure that the person.id is correctly set as the key at ...

In an Electron-React-Typescript-Webpack application, it is important to note that the target is not a DOM

Rendering seems to be working fine for the mainWindow. webpack.config.js : var renderer_config = { mode: isEnvProduction ? 'production' : 'development', entry: { app: './src/app/index.tsx', //app_A: './src/a ...

Definitions for TypeScript related to the restivus.d.ts file

If you're looking for the TypeScript definition I mentioned, you can find it here. I've been working with a Meteor package called restivus. When using it, you simply instantiate the constructor like this: var Api = new Restivus({ useDefaultA ...

Utilizing React's idiomatic approach to controlled input (leveraging useCallback, passing props, and sc

As I was in the process of creating a traditional read-fetch-suggest search bar, I encountered an issue where my input field lost focus with every keypress. Upon further investigation, I discovered that the problem stemmed from the fact that my input comp ...

The Angular application being developed will load JavaScript and CSS files relative to the current path

When ng serve is running and reloading the app on changes, it attempts to load resources (*.bundle.js) relative to the current path. This leads to a series of 404 errors if the current path is not the root of the app. The <base href="/"> element se ...

Forget it, Access Denied

Recently, I started using nvm to handle multiple node versions, but things took a turn for the worse when I attempted to install a wrong dependency on my Angular 5 application. Instead of ng2-redux, I accidentally installed ng-redux. Now, I'm faced w ...

Error encountered during ng update process: The workspace root directory is unable to resolve the "@angular-devkit/schematics" package

ng update The error message stating that the "@angular-devkit/schematics" package cannot be found in the workspace root directory suggests a possible issue with the node modules structure. To address this, you can start by deleting bo ...

Organizing components that display slightly more intricate data: Choosing between storing data, passing props, or combining into a single component

I am looking to develop a component that enables users to edit data for a mathematical classification model. The interface concept may resemble the following: interface MathModel { name: string; article: ArticleDescription; criteria: Criteria; coe ...

Are you encountering issues with Google Analytics performance on your Aurelia TypeScript project?

I recently started using Google Analytics and I am looking to integrate it into a website that I'm currently building. Current scenario Initially, I added the Google Analytics tracking code to my index.ejs file. Here is how the code looks: <!DOC ...

Clear a form field automatically in React using react-hook-form when a new value is entered in another field

Featuring this particular component: import { yupResolver } from '@hookform/resolvers/yup'; import { useForm } from 'react-hook-form'; import * as yup from 'yup'; import { Input, TabsGroup, Button } from './ui-components ...

When a type is exported from a Typescript module, it can be of

I am facing an issue with exporting a complex type created using the typeof operator in Typescript. Here is the scenario: // controller/createProfile.ts import { z } from 'zod'; import { zValidator } from '@hono/zod-validator'; const ...

How do I disable the floating label feature for Material Select in Angular 15?

This issue suggests the possibility of disabling the float feature for Angular Material. I attempted the following solution: .mat-form-field-can-float.mat-form-field-should-float .mat-form-field-label, .mat-form-field-can-float .mat-input-server:focus + ...

Is it possible to utilize Angular without the need for Node.js?

In our project, we have been utilizing Django as our server framework and incorporating JavaScript for client-side scripting. As we transition to Angular4, we are considering the necessity of running a separate node.js server alongside our current Django ...

The 'target' property is not found on the specified 'string' type

I've encountered an issue while creating a search bar in Typescript. Despite my efforts, the input is not being recognized. It seems that whenever I use the term 'target' in my onChange method, it triggers an error stating: Property &ap ...

Troubleshooting mistakes in Typescript import syntax and beyond

During the development of this project in react and typescript using create-react-app, I encountered no issues. Now, my aim is to publish one of the components on npm. I have come to understand that I need to build this component separately from an existi ...

The header grid will disappear in the p-dialog when the browser is minimized

Check out this informative article on creating a dynamic dialog box using PrimeNG here Take a look at the image below to see how the dialog box appears when opened: https://i.sstatic.net/nUq1d.png One issue I am facing is that the grid header gets hidd ...

Utilizing React TypeScript to implement onClick functionality within a map iteration

Encountering an error with onClick within a map in the code, specifically: Type 'void' is not assignable to type '((event: MouseEvent<HTMLDivElement, MouseEvent>) => void) | undefined'.ts(2322) index.d.ts(1348, 9): The expecte ...

Using conditional statements to localize in Angular

In my Angular application, I am utilizing i18n for internationalization purposes. When it comes to HTML, I use i18n="@@<unique_Id>" and for dynamic elements defined in TypeScript class files, I use $localize. For example: this.buttontext = ...