Angular 12 web version displays error message: "404 not found" for the requested URL

I recently completed my first website using Angular and uploaded it to the server successfully. When browsing through the pages, everything seems fine. However, I encountered an issue when trying to access specific URLs by copying and pasting them into the browser. For instance, if I paste the URL 'www.project.com/app/home/users/22' directly, I receive a '404 not found' error. But if I navigate to the 'user 22' link from the homepage URL 'www.project.com/', it works perfectly. Can anyone explain why this is happening and how I can resolve it?

Thank you

app-routing.module.ts


    import { NgModule } from '@angular/core';
    import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
    import { AuthGuard } from "./guards/auth.guard";
    import { GuestPageModule } from "./pages/guest/guest.module";

    // Define route paths
    const routes: Routes = [
      // Redirect to welcome page
      { path: '', redirectTo: 'welcome', pathMatch: 'full'},
      // Load the welcome module with authentication guard
      { path: 'welcome', loadChildren: () => import('./pages/welcome/welcome.module').then(m => m.WelcomePageModule) },
      
      // Other route definitions...
    
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
  

The URL I'm trying to reach is

Link example:

<a (click)="goUser(user)">
    
    // Function to handle user navigation
    goUser(user: any) {
      user = user;
      this.userData.userViewed(user).pipe(
        map((data: any) => {
          if (data.success) {
            const navigationExtras: NavigationExtras = {
              state: {
                user: user
              }
            };
            // Navigate to the user details page
            this.router.navigate(['guest/user' + id], navigationExtras);
          }
        })
      ).subscribe()
    }
  

Guest routing module:


    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';

    import { GuestPage } from './guest.page';

    const routes: Routes = [
      // Basic route configuration for guest page
      
      // Other nested routes...
    
    ];

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

Answer №1

It appears that there may be an issue with the lazy loading implementation in the guest module.

  {
    path: 'guest',
    loadChildren: () => import('./pages/guest/guest.module').then(m => m.GuestPageModule)
  }

For more information, please refer to the documentation at: https://angular.io/guide/lazy-loading-ngmodules#create-a-feature-module-with-routing

Additionally, it seems unusual that there is no leading slash / before the id - double check if the slash is included in the id variable.

this.router.navigate(['guest/user' + id], navigationExtras);

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

How can one retrieve the selected value from a dropdown menu in Angular?

Objective: My goal is to create a dropdown menu where users can select a value, which will then dynamically change the address of the website based on their selection. Issue: Although I managed to make the address change upon selection, it did so for ever ...

Adjust the alignment of divs in Angular

In developing a dashboard, I have successfully displayed key value pairs in a component by retrieving them from an environment.ts file. Now, I am looking to rearrange the positioning of the individual testcard components to align with a specific layout sho ...

When working with React and Typescript, I encountered an issue with refs causing a typescript error: "The property 'current' does not exist on the type '(instance: HTMLInputElement | null) => void'"

I am working on a component that requires a ref prop: import React, {forwardRef, ReactElement, ReactNode, HTMLProps, useRef} from 'react' import styles from './index.module.css' import classNames from 'classnames' import { Ico ...

Is it possible to close a tab while the chrome extension popup is still active?

I am currently developing a Chrome extension that reads the content of the current tab and performs a heavy task on the backend. If I were to close the tab while the process is ongoing, how can I allow the user to do so without waiting for the task to fi ...

Creating a Personalized Color Palette Naming System with Material UI in TypeScript

I have been working on incorporating a custom color palette into my material ui theme. Following the guidance provided in the Material UI documentation available here Material UI Docs, I am trying to implement this feature. Here is an excerpt from my cod ...

Is it possible to implement typed metaprogramming in TypeScript?

I am in the process of developing a function that takes multiple keys and values as input and should return an object with those keys and their corresponding values. The value types should match the ones provided when calling the function. Currently, the ...

Accessing element from view within controller in Ionic version 3.5

I am currently working on a project in Ionic 3.5, where I need to implement a feature that automatically loads an image "ad" after the page finishes loading. Right now, clicking a button successfully displays the image. However, I want this functionality ...

Leverage TypeScript AngularJS directive's controller as well as other inherited controllers within the directive's link function

I am currently developing an AngularJS directive in TypeScript for form validation. I am trying to understand how to utilize the directive's controller and inherit the form controller within the directive's link function. Thank you in advance! ...

Troubleshooting: Issue with Angular 2 FormArray

Hey there! I'm currently working on my Angular 2 Recipe app where I want to display multiple ingredient details. I am using a FormArray but encountered an error while debugging with the browser developer tools. The error displayed on the Console tab i ...

Utilize a generic approach for every element within a union: Transforming from Some<1 | 2 | 3> to individual Some<1>, Some<2>, or Some<3> instances

As I was unable to create a concise example for my issue, this is a general rendition of it. I am facing a scenario where the "sequence of application" is vital in nested generics. type Some<A> = {type: A} type Union1 = Some<1 | 2 | 3> type Uni ...

Transfer a reference from one list to another within the Redux store

Within my store, I have various lists that contain unique identifiers referencing normalized entities. The structure is typically as follows: { list1: [ 1 ], list2: [], //other lists entities: { 1:{data}, ... } } Users have the abil ...

React input delay handling during onChange event

Upon closer inspection, I've come across an issue that I had not previously noticed. I am unsure if there is a bug within my code or if the onChange function always behaves in this manner, but I am experiencing input delay and am uncertain on how to r ...

Aggregating all elements in an array to generate a Paypal order

I'm currently working on a React project where I need to integrate the PayPal order function and include every item from an array. Below is my code snippet, but I'm struggling with how to achieve this: export default function Cart(): JSX.Element ...

The ViewChild property encountered an error: "The child element cannot be found"

I am facing an issue in Angular 7 where I have a parent component with a child component. I would like to obtain a reference to the child component within the parent component so that I can access its functions and properties. Child Component : @Componen ...

Determining the parent type in Typescript by inferring it from a nested member

Typescript has the ability to infer the type of a value based on queries made within if statements. For instance, the type of one member of an object can be deduced based on another: type ChildType = 'a' | 'b'; type Child<T extends ...

Shifting Angular Component Declarations to a New Location

Here's a question that might sound silly: In my Angular project, I am looking to reorganize my component declarations by moving them from angular.module.ts to modules/modules.modules.ts. The goal is to structure my src/app directory as follows: src ...

Choose several checkboxes from the dropdown menu

After clicking the dropdown, I want to select multiple checkboxes instead of just one. I have tried using the prevent default method but it did not work. Beginner concept ...

Efficient code for varying layouts of disabled Angular Material buttons

I've been wondering if there's a simpler way to customize the appearance of a disabled button using Angular Material. Currently, I achieve this by utilizing an ng-container and ng-template. While this method gets the job done, the code is not ve ...

Can the hexadecimal value from an input type color be extracted and used to populate form fields that will then be displayed in a table after submission?

Hello everyone, I'm new to this platform and seeking guidance on how to improve my post! I recently created a CRUD app using Angular. It consists of a basic form with 4 fields, a color picker using input type='color', and a submit button. U ...

Changing the color of the bullet icon in Angular material stepper based on a condition

Currently, I am working with the vertical stepper component from Material Angular. I am wondering how I can change the color of each icon based on a specific condition. If you want to take a look at my code, here is the StackBlitz link. Here is an exampl ...