Why is it that in Angular my component's HTML body refuses to display?

My experience with navigating to the component has been smooth, as both the header/sidebar load properly and the component typescript functions correctly.

However, I am facing an issue where none of the HTML is being displayed, specifically when accessing the dashboard/:id route.

Do you have any suggestions on how to troubleshoot this?

app-routing.module.ts:

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

import { LoginComponent } from './login.component';
import { DashboardComponent } from './views/dashboard/dashboard.component';

// Import Containers
import { DefaultLayoutComponent } from './containers';

// AuthGaurd
import { IdguardGuard as IdAuthGaurd } from './idguard.guard';
import { AdminGuard as AdminGuard } from './admin.guard';
import { ClientgaurdGuard as ClientAuthGaurd } from './clientgaurd.guard';


export const routes: Routes = [
  {path: 'login', component: LoginComponent},
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full',
  },
  {
    path: '',
    component: DefaultLayoutComponent,
    data: {
      title: 'Home'
    },
    children: [
      {
        path: 'dashboard/:id',
        component: DashboardComponent,
        canActivate: [IdAuthGaurd, ClientAuthGaurd]
      },
      {
        path: 'base',
        loadChildren: () =>
          import('./views/base/base.module').then((m) => m.BaseModule),
      }
    ]
  }
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {
    scrollPositionRestoration: 'top',
    anchorScrolling: 'enabled',
    relativeLinkResolution: 'legacy'
}),
  ],
  exports: [RouterModule],
})
export class AppRoutingModule {}

Answer №1

Redirecting to home is not possible as there is no home path in the same level:

Modify it to:


export const routes: Routes = [
  {path: 'login', component: LoginComponent},
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full',
  },
  {
    path: 'home',
    component: DefaultLayoutComponent,
    data: {
      title: 'Home'
    },
    children: [
      {
        path: 'dashboard/:id',
        component: DashboardComponent,
        canActivate: [IdAuthGaurd, ClientAuthGaurd]
      },
      {
        path: 'base',
        loadChildren: () =>
          import('./views/base/base.module').then((m) => m.BaseModule),
      }
    ]
  }
];

Then proceed to navigate to /home/dashboard/:id.

Alternatively:


export const routes: Routes = [
  {path: 'login', component: LoginComponent},
  {
    path: '',
    component: DefaultLayoutComponent,
    data: {
      title: 'Home'
    },
    children: [
      {
        path: 'dashboard/:id',
        component: DashboardComponent,
        canActivate: [IdAuthGaurd, ClientAuthGaurd]
      },
      {
        path: 'base',
        loadChildren: () =>
          import('./views/base/base.module').then((m) => m.BaseModule),
      }
    ]
  }
];

This adjustment will ensure seamless navigation. Just head over to /dashboard/:id.

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

Errors may arise in the Typescript compiler when node.d.ts is included

After recently making the switch from PHP to Node.js, I decided to use Typescript along with Node.js as I am a big fan of Typescript. Initially, everything was working smoothly with my sample code but when I began scaling up and expanding my project, I enc ...

Guide to implementing the Angular Leaflet directive with Typescript

Could someone guide me on how to integrate `angular-leaflet-directive` into my typescript project? What steps should I follow for this process? Do I need a type definition file specifically for `angular-leaflet-directive`, or is having one for `leaflet` ...

Casting types of objects in Angular2 using TypeScript

Trying to extract the model object from the JSON obtained through a http response call. The following JSON represents the body of the http response, { "dataType": "RWSupplier", "partyName": "Lifecare Pharmaceuticals", "partyShortName": null, "partySecon ...

Connecting Angular modules via npm link is a great way to share common

Creating a project with a shared module that contains generic elements and components, such as a header, is my goal. This shared module will eventually be added as a dependency in package.json and installed through Nexus. However, during the development ph ...

Express routes are malfunctioning

I have a situation with two different routes: /emails and /eamils/:id: function createRouter() { let router = express.Router(); router.route('/emails/:id').get((req, res) => { console.log('Route for get /emails/id'); }); ...

Leverage the power of React, Material-UI, and Typescript to inherit button props and incorporate a variety of unique

Looking to enhance the Material-UI button with additional variants like "square." How can I create a prop interface to merge/inherit props? Check out the following code snippet: import React from "react"; import { Button as MuiButton } from "@material-u ...

Crafting a model for arrays of objects - a guide to perfection

Why am I experiencing errors in the console when trying to set the API return to a variable? How can this issue be resolved? This is my TypeScript code: public myData = new myDataModel(); getData(){ this.myCoolService.getDataAPI() .subscribe(( ...

Angular 8 dropdown menu that closes when clicking outside of it

Hello, I am currently using the click function on the p tag. When a user opens the dropdown menu, I want to close it when they click outside of it in Angular. Here is the HTML code: <div class="select_cat"> <p class="cat_name" (click)="openC ...

What is the best way to ensure that each service call to my controller is completed before proceeding to the next one within a loop in Angular?

Calling an Angular service can be done like this: this.webService.add(id) .subscribe(result => { // perform required actions }, error => { // handle errors }); // Service Definition add(id: number): Observable < any > { retu ...

Experience the magic of changing backgrounds with Ionic 4's dynamic image feature

I am currently facing an issue while attempting to add multiple background images in my Ionic 4 project. I have successfully created styles for static images, but when it comes to dynamic images, I encounter errors with the styles. <ion-content st ...

Duplicate Subscription Issue with Angular Material Flex MediaObserver

Utilizing Material Flex Layout for screen size detection, I encountered a peculiar issue where the subscribed result was appearing twice. Interestingly, the same code in another Angular project produced the expected outcome. constructor( public mediaO ...

Guide on implementing JWT authentication in API using Nebular Auth

I have implemented Nebular auth for my Angular application. I am trying to include a token in the header when a user logs in. Below is the API response: { "status": 0, "message": null, "data": { "type": &qu ...

Having trouble retrieving data from a JSON file within an Angular application when utilizing Angular services

This JSON file contains information about various moods and music playlists. {mood: [ { "id":"1", "text": "Annoyed", "cols": 1, "rows": 2, "color": "lightgree ...

Protractor encounters an error stating "No element found with specified locator" after attempting to switch to an

I've been attempting to download an embedded PDF from a webpage using Protractor selenium. However, I seem to be stuck when it comes to actually downloading the file as I always encounter the following error: Failed: No element found using locator: ...

What is the proper way to add additional properties to an array object when initializing it in TypeScript?

Is there a more elegant solution for creating an object of type: type ArrayWithA = [number, number, number] & { a: string }; I accomplished this by: const obj : any = [1, 2, 3]; obj.a = "foo"; const arrayWithA : ArrayWithA = obj as ArrayWith ...

The changes made to the path property in tsconfig.json are not being registered

A troublesome block of imports has surfaced, as shown in this image: https://i.sstatic.net/lRwK5.png Below is my current configuration in tsconfig.json : { "compilerOptions": { "target": "es5" /* Specify ECMAScript targ ...

What is the best method for embedding my token within my user entity?

Currently, I am working on implementing a "forgot password" feature in my application. The idea is that when a user requests to reset their password, they will receive a token via email that expires after two hours. To prevent the generation of multiple to ...

What is the best way to generate an index.ts file in an Angular 14 shared library that exports all contents from a specific directory?

Utilizing Angular 14 for my shared library project, the structure looks like this: + projects + my-lib - package.json + src - public-api.ts + lib + helpers - index.ts ...

Angular Universal app experiencing "JavaScript heap out of memory" error in Docker container following several days of operation

I recently converted my Angular application to Angular Universal. It's built on Angular 15 and is running in a Docker container. I start the server using the command "npm serve:ssr". Everything works fine for a day or two, but then it starts throwing ...

Creating a CSS animation to slide a div outside of its container is

I currently have a flexbox set up with two adjacent divs labeled as DIV 1 and DIV 2. By default, both DIV 1 and DIV 2 are visible. DIV 2 has a fixed width, occupying around 40% of the container's width. DIV 1 dynamically adjusts its width to ac ...