Displaying a page within another page using Angular routingTo feature a page within another

I am in the process of building a new project for myself. My goal is to create a component that includes a navbar, sidebar, footer, and a page to be displayed as a router.

Below is my app.routing setup:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LayoutComponent } from './layout/layout.component';
import { LoginComponent } from './screens/auth/login/login.component';
import { StatsComponent } from './screens/stats/stats.component';

const appRoutes: Routes = [
  { path: '', component: LoginComponent },
  { path: 'Dashboard', component: LayoutComponent },
  { path: 'login', component: LoginComponent },
  { path: 'stats', component: StatsComponent }
];
@NgModule({
  imports: [RouterModule.forRoot(appRoutes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

I have a basic login page which then directs me to the dashboard route.

<h1>Angular Router App</h1>
<nav>
  <ul>
    <li><a routerLink="/Dashboard/login" routerLinkActive="active">Login</a></li>
  </ul>
</nav>

This is my LayoutComponent (Dashboard):

<div class="wrapper">
    <div class="app-sidebar" data-active-color="white" data-background-color="black" data-image="assets/img/sidebar-bg/01.jpg">
        <p>Sidebar</p>
         <div class="sidebar-background"></div>
     </div>
     <p>NavBar</p>
     <div class="main-panel">   
         <div class="main-content">
             <div class="content-wrapper">
                 <div class="container-fluid">
                     <router-outlet></router-outlet>
                 </div>
             </div>
         </div>
         <p>Footer</p>
     </div>
 </div>
 
 

You'll notice I have

<router-outlet></router-outlet>
on this page. I'm looking to understand how I can display another component within this layout. Specifically, I have a component called stats in my app-routing. How can I show this component within the layout using routing?

For example, something like Dashboard/stats.

Answer №1

When incorporating routing into your project, the first step is to create the project with the following command:

ng new my-project --routing=true

This will generate a necessary routing file named app-routing.module.ts

To add a route, execute the command below:

ng generate module routes/route-1 --routing=true

This action will create additional files, specifically:

route-1.module.ts route-1-routing.module.ts

In route-1-routing.module.ts, define the routes as shown below:

const routes: Routes = [
  { path: '', component: Route1Component },
  { path: 'hello', component: HelloComponent }
]

(Remember to create these components using commands like

ng g c routes/route-1/components/route-1
and
ng g c routes/route-1/components/hello
- similar to the ng new command)

Incorporate the new route in app-routing.module.ts by adding the following:

const routes: Routes = [
  {
    path: 'route-1',
    loadChildren: () => import('./routes/route-1/route-1.module').then(m => m.Route1Module)
  }
]

You can now navigate to:

/route-1 in your browser to view Route1Component (ensure

<router-outlet></router-outlet>
is present - remember to include router-outlet in app.component.html)

and visit:

/route-1/hello to see HelloComponent (again ensure router-outlet is included)

Lastly, you can create links for your routes using:

<a routerLink="/route-1">route-1</a>
<a routerLink="/route-1/hello">route-1/hello</a>

Repeat this process to incorporate additional routes.

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

Error when accessing JSON property in Angular with Ionic 2: A Strange TypeError

I have implemented a provider in my Ionic project to fetch the user object from storage. Below is the code: import { Injectable } from '@angular/core'; import { Storage } from '@ionic/storage'; @Injectable() export class ApiProvider { ...

What is the best way to dynamically determine the base path for my templates in Angular 2?

Is it possible to dynamically define the base path of two versions of each template in order to use one or the other through configuration? How can I declare TEMPLATES_PATH so that it can be implemented as shown below: component.ts @Component({ temp ...

Working with objects in *ngFor in Ionic 2

I am utilizing Ionic 2 and attempting to display the content of a key-value array using an object. In order to display my collection, I am using a pipe in my HTML. Below is my HTML code: <ion-list> <ion-item *ngFor="let event of this.pdata. ...

How to send empty values in Angular4 HTTP post request to web API

Attempting to call an http post method from an Angular 4 component to a web API is resulting in empty values being returned. Even when checking the request using postman, the values are still empty. Here is the http post call from the component file: Ed ...

A guide to showcasing data within PrimeNG's pie chart component using either JavaScript or Angular

I have utilized the pie chart feature from the PRIMENG CHART PLUGIN My goal is to showcase the values within a pie chart Below, you can find my code for reference **Within app.component.html** <div style="display: block"> <p-chart type="pi ...

Incorporating Bloodhound into an Angular 2 CLI project

I have been working on integrating Bloodhound.js into an Angular 2 project that utilizes Angular 2 CLI. Currently, I have successfully implemented jQuery by following these steps: Installed jQuery using npm install jquery --save Installed jQuery Type ...

Showing the AngularFireList data on the HTML page following successful retrieval

Does anyone know how to display an AngularFireList on an HTML page? import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; import {AngularFireAuth} from 'angularfire2/auth'; import {AngularF ...

Combine arrays using union or intersection to generate a new array

Seeking a solution in Angular 7 for a problem involving the creation of a function that operates on two arrays of objects. The goal is to generate a third array based on the first and second arrays. The structure of the third array closely resembles the f ...

Angular: Extracting data from mat select by id

I'm in the process of designing a dynamic form that includes multiple dropdowns (mat-select). The challenge I am facing is that the select element only holds the key and not the actual value. This has left me unsure about how to display the selected v ...

Exploring the use of TypeScript and Webpack to read non-JavaScript files in Node.js

I'm working on a backend NodeJS setup written in TypeScript that is using webpack for compilation. However, I encountered an error when trying to read a text file even though I confirmed that the file source/test.txt is being copied to the build fold ...

Is there a way to configure the location of a dialogue box beneath the button click action in every row within a Mat table?

Check out my sample code here to see the working code I created. I attempted to position a dialog box using this code: dialogConfig.position = { top: '370px',left: '500px'} However, it opens at the same position for each row. View ...

Encapsulating functions with multiple definitions in Typescript

Struggling with wrapping a function that can have multiple return types based on input parameters in Typescript. Imagine wanting a function to return ReturnA for VariantEnum.a and ReturnB for VariantEnum.b. Consider this implementation of sampleFunction: ...

Innovative feature: Customizable dropdown options in PrimeNg for Angular 4

As a beginner in UI development and PrimeNg, I may be missing something obvious here. I am currently working on a ngFor loop to create multiple levels of dropdowns. For example, I have two rooms - Deluxe and Suite. The Deluxe room has rates for Breakfast, ...

Event-Propagation in Angular 5 with mat-expansion-panel within another component

In my project, I am facing a challenge where I need to create multiple mat-expansion-panels within one mat-expansion-panel. Everything works fine except for the issue that when I try to open a child-panel, it triggers the close-event of the parent-panel. ...

Switching an application from Angular 5 to Angular 8 can lead to unexpected styling problems

My current project is based on Angular version 5.2, but we recently decided to upgrade it to Angular 8 along with updating the Angular CLI. After completing the necessary code refactoring for Angular 8, the application builds successfully without any error ...

Encountering an issue in an Angular project where it remains stuck at the preloader stage

Whenever I attempt to redirect to the home page, the URL path changes but the preloader continues to load without rendering the home page until I hard reload the page. Even when I remove the preloader, the page still does not render until I hard reload it. ...

Utilize the data emitted by an Observable to produce a new Observable

Utilizing an Observable to toggle the visibility of a component and extracting data from it is my goal. I aim to avoid directly invoking the service within the child component to maintain its simplicity. @Component({ selector: 'my-component', ...

Exploring SVG Graphics, Images, and Icons with Nativescript-vue

Can images and icons in SVG format be used, and if so, how can they be implemented? I am currently utilizing NativeScript-Vue 6.0 with TypeScript. ...

Error: The `ngMetadataName` property cannot be accessed because it is undefined or null in Internet Explorer version 10

Encountered an issue in IE 10 that is not present in IE 11: Error: TypeError: Unable to get property 'ngMetadataName' of undefined or null reference The property ngMetadataName can be found in the file vendor.js. This is the content of polyf ...

Unable to render information within the PrimeNG tree component

I've set a goal for myself to create a treeview using the PrimeNG Tree Component. Currently, I have a small service with the following method: TypeScript: getMenuDetails(parentID: number) { let url = this.serverURL + 'api/Nodes/' + pa ...