"Two distinct routes in Angular, but keeping the same menu item highlighted as

In my app-component.ts, I have a menu structured like this:

<ul>
  <li>
    <a href="#" routerLink="/" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">Home</a>
  </li>
  <li>
    <a href="#" routerLink="/shop" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">Shop</a>
  </li>

</ul>
<router-outlet></router-outlet>

I also have a route file with the following paths and components:

export const appRoutes: Routes = [
  {
    path: '',
    component: HomeComponent
  },
  {
    path: 'shop',
    component: ChildComponent
  },
  {
    path: 'details',
    component: DetailComponent
  }
];

Currently, the active state in the menu correctly selects the "Shop" item, but when navigating to the "Details" component from the shop page, the state is no longer active. Is there a way to maintain the active state in the menu for both components even while on another route?

You can see an example of this issue here:

https://stackblitz.com/edit/parent-child-active-x6sp1e

Answer №1

Based on information from the Angular Documentation, it is recommended to structure your appRoutes as shown below:

const routes: Routes = [
{ 
 path: 'shop',
 component: ChildComponent, 
  children: [
    {
      path: 'details', 
      component: DetailComponent
    }
   ] 
 }

To make this setup work effectively, make sure you have two separate

<router-outlet></router-outlet>
elements. One should be placed within your Home component and the other within your Shop component.

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

Encountering a 500 Internal Server Error while making a call to a RESTful (GET) Web

Currently, I have a setup where my web server is housed inside a virtual machine, with the client located outside of it. On the server side, I am utilizing .NET Framework 4.6.1 for development. Below is the architecture of my WEB API controller on the se ...

refreshing the webpage's content following the completion of an asynchronous request

I am working on an Ionic2 app that utilizes the SideMenu template. On the rootPage, I have the following code: export class HomePage { products: any = []; constructor(public navCtrl: NavController, public navParams: NavParams, private woo: WooCommer ...

Error TS6059 in Angular monorepo: The file 'ng-youtube-api.service.ngtypecheck.ts' is not located within the 'rootDir' directory. The 'rootDir' should include all source files

Creating multiple Angular libraries/packages is crucial for organizing code efficiently. In this case, I have developed three distinct packages: @example/ng-youtube-player: includes a YoutubePlayerComponent and the associated YoutubeApiService @example/ng ...

The progress bar is only displayed during the initial consecutive file uploads in Angular 6

Within my Angular application, I have implemented a dedicated form for uploading profile pictures. The process involves triggering a confirmation modal upon uploading a new image, followed by the display of a progress bar on the page. This progress bar ind ...

Exploring the Benefits of Using Relative Image Paths in Angular 2 and ASP.NET Core

I'm having trouble locating the relative paths for local image files in Angular 2. Typically, I would access them from the wwwroot/images folder, but when I try to load them from there it doesn't work. Where can I find the relative paths in Angu ...

Issue encountered when attempting to import a module within the ionic framework

I encountered an issue in my project (built with the ionic-framework 3) where I included the following line to import the dialogflow module: const dialogflow = require('dialogflow'); However, when compiling, it resulted in the error message: ...

Unpacking Constructor with visible arguments

In my constructor, I utilize destructuring to simplify the parameters needed to create an object with default values. export class PageConfig { constructor({ isSliding = false }: { isSliding?: boolean; getList: (pagingInfo: PagingInfo) =&g ...

Angular component nesting involves placing one component within another component in

When a component is nested inside another, what is that called? For example: <agm-map [latitude]="lat" [longitude]="lng"> <agm-marker [latitude]="lat" [longitude]="lng"></agm-marker> </agm-map> Can you explain how this nesting w ...

Dynamically determine the length of an array using Typescript

Can the length of an array be determined based on a numerical variable? For example: func(1); // outputs [string] func(2); // outputs [string, string] func(5); // outputs [string, string, string, string, string] ...

Incorporate the NestJS module seamlessly into one module from another module without the need to import it

As a newcomer to nestJs, I am currently in the process of creating a module named example.module. Within this module, I am importing another module called DB.Module. However, I have encountered an error when I do not import DB.Module in App.Module. My conc ...

Establishing a component variable based on data received from an observable

I am attempting to connect a variable in a component to the content retrieved from an observable within a subscribe() function using an anonymous arrow function: ngOnInit() { this.strSub = this._store.select(selectStr).subscribe((data) => console.lo ...

Every variable declaration that follows must match the data type of the initial declaration

Here's the link I'm using to kickstart a sample node js project with webworker-thread: https://www.npmjs.com/package/webworker-threads Below is my TypeScript code snippet: var Worker = require('webworker-threads').Worker; var worker ...

Difficulty encountered when hosting an Angular and .Net Core application on a virtual machine's localhost

Having trouble running an application from a virtual machine on Azure? I'm facing an issue that I can't seem to solve. The application's front end is Angular and the backend is .NET Core 3.1. I'm using ngrok to tunnel my virtual machine ...

The navigation function in Angular, this.router.navigate, is causing issues and

I've encountered a peculiar issue. There's a logout function that is activated whenever I receive a 401 response from any API I interact with. The function looks like this: constructor( private router: Router, ) {} logout(router1: Router ...

Tips for obtaining the unique array element lengths in a JSON array

How can I determine the number of active users in a JSON array that includes both active and inactive user statuses? { "body": { "existing_users": [ { "product": "Pack-2", "user_statu ...

Angular Bootstrap multi-typeahead component glitches

Currently, I am utilizing the bootstrap angular typehead feature. When using it for a single input field, everything functions as expected. However, upon attempting to implement multiple instances, I encounter an issue where one dropdown interferes with th ...

Module for migration located in a subdirectory

Currently, I am in the process of transitioning an Angular application to NativeScript while utilizing a code-sharing setup. For the migration of Angular modules, I have been executing the following command: ng g migrate-module --name=nameModule However ...

The error at events.js:154 was not properly handled and caused an 'error' event to be thrown

Encountered an error while creating an Angular 2 application. I followed the instructions from this link to create a sample application. Upon running npm start Received the following error, events.js:154 throw er; // Unhandled 'error' even ...

Issues with Angular2 Router functionality not functioning as expected

I have been facing an issue while trying to set up a basic Angular2 application with login functionality using Typescript. Despite defining the Router, I encounter an error when trying to access the specified URL in a browser. The error message reads: Can ...

Encountered an error in React where the declaration file for a module could not be located

New to Typescript and trying to incorporate a splitter into my project. I'm utilizing SplitPane from "react-split-pane/lib/SplitPane" and Pane from "react-split-pane/lib/Pane" in my Typescript project, but encountering an error: Could not find a de ...