Child router receives subscription from parent router

Is there a way to trigger events on every URL change within a child router? I have been able to implement event firing on URL changes within the main router using the following code, but I am struggling with achieving this in a child router.

I'm currently working with router-deprecated and Angular2 beta versions, not RC.

// myapp.component.ts
@RouteConfig([
  {
    useAsDefault: true,
    name: 'Main',
    path: '/...',
    component: MainComponent,
  },
])
export class MyAppComponent {
  constructor(private _router: Router) {
    _router.subscribe((url: string): void => {
      // Trigger event on every URL change
      // e.g. from "/" to "/sub1", or "/sub1" to "/sub2"
    });
  }
}

// main.component.ts
// URL: /
@RouteConfig([
  {
    useAsDefault: true,
    name: 'Sub1',
    path: '/sub1',
    component: Sub1Component,
  },
  {
    name: 'Sub2',
    path: '/sub2',
    component: Sub2Component,
  },
])
export class MainComponent {
  constructor(private _router: Router) {
    _router.subscribe((url: string): void => {
      // Event not triggered...
    });
  }
}

// sub1.component.ts
// URL: /sub1
export class Sub1Component {}

// sub2.component.ts
// URL: /sub2
export class Sub2Component {}

(Please note that import syntax and Decorators were omitted in the above code.)

Answer №1

Using _router.parent.subscribe exceeded my expectations! Special shoutout to @yuzuri for the help.

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

Form appears outside the modal window

I am facing an issue with my modal where the form inside is displaying outside of the modal itself. Despite trying to adjust the CSS display settings and switching to react-bootstrap from regular bootstrap, the problem persists. I am uncertain about what s ...

Modifying the version target of TypeScript code results in the TypeScript Compiler being unable to locate the module

After installing signalr via npm in Visual Studio 2019, I encountered an issue. When the target in my compiler options is set to ES6, I receive the error TS2307 (TS) Cannot find module '@microsoft/signalr.'. However, when I change the target to E ...

What sets apart a JSON Key that is enclosed in double quotes "" from one that has no quotes?

Below is my TypeScript object: { first_name:"test", last_name: "test", birthdate:"2018-01-08T16:00:00.000Z", contactNumber: "12312312312", email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e19 ...

Angular chat fuze template fails to refresh conversation after implementing websocket functionality

In my Angular application, I have implemented a chat service that updates incoming chats in real-time using websockets. The websocket sends me the message and phone number, which I then use to update the chat. updateChatMessages(phoneNumber: string, messa ...

Issue arises when integrating Angular 13 with ngrx and OAuth2: encountering difficulties in creating an effect

Currently, I'm following an example that uses an earlier version of Angular and ngrx. You can find the tutorial on NGRX Authentication in Angular with asp.net core here. This is what my Auth.action.ts file looks like: import { createAction, props } f ...

Showing Nested Arrays in Angular 2

If I have an array with image links as shown below, how can I display them in HTML? array = [ { img: [ {0: 'http://hairsalonfurniture.eu/wp-uploads/750x480_how-to-create-a-nice-hair-salon-s-reception-gjzd.jpg',}, {1: 'http ...

Material UI offers a feature that allows for the grouping and auto-completion sorting

I am currently utilizing Material UI Autocomplete along with React to create a grouped autocomplete dropdown feature. Here is the dataset: let top100Films = [ { title: "The Shawshank Redemption", genre: "thriller" }, { title: " ...

The double deletion of all input causes a glitch in the two-way binding functionality

I attempted to prevent an input from being empty, but found that when using the method of selecting all text and then pressing backspace, the input only updates every other attempt. To see this issue in action visit: https://stackblitz.com/edit/angular-em ...

Testing Angular Components with Jasmine and Karma: When handling the 'onChange' event, the changeEvent parameter of type MatRadioChange should not be void and must be assigned to a parameter of type

Hey there, I was working on a test for a call where I am using to emit the event: onChange(eventName: MatRadioChange): void { this.eventName.emit(eventName.value); } Here is the test I have written for it: describe('onChange', (eventName: ...

Is it necessary for a TypeScript Library's repository to include the JavaScript version?

Is it necessary to include a JavaScript version of the library along with the Typescript repository for consumers? Or is it best to let consumers handle the compilation process themselves? Or should I consider another approach altogether? ...

An issue with TypeORM syntax causing errors within a NestJS migration file

I recently encountered an issue while setting up PostgreSQL with NestJS and TypeORM on Heroku. Despite successfully running a migration, my application kept crashing. I attempted various troubleshooting methods by scouring through blogs, GitHub issues, and ...

The display of the key property is missing on the rendered page in a React TypeScript project

While working with Typescript React, I encountered an error message: react-jsx-dev-runtime.development.js:87 Warning: Each child in a list should have a unique "key" prop. Even though I included a key in the li tag like this: const MyMenuItem: ...

Looking for a regular expression to require either a dollar sign ($) or a percentage symbol (%) but not

At the moment, I currently have this input field set up on an HTML page within my Angular 9 application: <input type="text" formControlName="amountToWithholdInput" onkeyup="this.value = this.value.replace(/[^0-9.%$]/, &ap ...

Implementing drag and drop functionality for dynamically generated components in Angular 7

I have a follow-up question based on my previous inquiry posted on Stack Overflow: Add directives to component selector when it is declared - Angular 7 My current scenario involves dynamically generating components upon clicking a button. These components ...

Assistance needed: How to add extra information when registering a user on JHipster?

After diligently following this tutorial to add more information about the user, I encountered an issue while trying to include an input field in the register.component.html. As soon as I added the ngModel directive, the page stopped functioning properly. ...

Retrieve the template parameter from a generic type

While I have experience extracting string from string[], this particular scenario is proving to be quite challenging: type example<T = boolean> = true; // with just "example", how can I retrieve the template parameter "boolean" in this case? type T ...

Issues arise when attempting to store data into an array with the assistance of FileReader

I am currently working on an Angular4 project where I am facing an issue with saving Blob data returned from my API call to an array of pictures in base64 format. This is so that I can later display the images using *ngFor. Here is the API call I am makin ...

Trouble transferring image file to server using Ionic 3 app

Currently, I am working on an app in Ionic 3 with a workflow that allows users to upload pictures either from the camera or gallery. There are two scenarios involved: In the first scenario, you can choose the camera and directly upload the image to the se ...

Refreshing Datatable in Angular 2 - Trouble with dtInstance.then error

I'm currently working on an Angular 2 application where I have a component that includes both a dropdown list and a datatable. The requirement is to display details in the table based on the name selected from the dropdown list. HTML - <div> ...

Encountering this issue? Error TS2304: Name not found

When I attempt to run unit tests for my Angular application using 'ng test', I encounter the error message: "Error TS2304: Cannot find name.." Interestingly, running 'ng serve' works without any issues. I followed a guide (link provide ...