Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

445
Visualizações
In Angular 13 why does my route subscription stop it hits a route without a param?

I need a way to update a variable in my header component (always displayed) whenever a param changes in the URL.

My routing:

const routes: Routes = [
  {
    path: '',
    component: DefaultLayoutComponent,
    data: {
      title: 'Home'
    },
    children: [
      {
        path: 'home',
         component: HomeComponent
      },
      {
        path: 'dashboard/:id',
        component: DashboardComponent,
      },
    ]
  },
  {path: '**', redirectTo: 'home'}
];

The dashboard component has an ID attached to it and this is what I need to track. In the event a user goes to a page without an ID (such as the home route) I want the ID to show as 0.

I have this working to where it will track the ID as long as they continue to hit routes with an ID but as soon as they navigate to home all tracking is stopped.

header.component

import { Component, Input } from '@angular/core';
import { ClassToggleService, HeaderComponent } from '@coreui/angular-pro';
import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';  

@Component({
  selector: 'app-default-header',
  templateUrl: './default-header.component.html',
})
export class DefaultHeaderComponent extends HeaderComponent {

  private issuer: Subscription;

  constructor(private classToggler: ClassToggleService,
    private route: ActivatedRoute) {
    super();
  }

  issuerID: number;


  ngOnInit(): void {
    this.route.children.forEach(child => {
      child.params.subscribe(params => {
          const id = params['id'] || 0
          console.log('id ' + id)
          this.issuerID = id
      })
    })
  }
  
     //To prevent memory leak
     ngOnDestroy(): void {
      if (this.issuer)
        this.issuer.unsubscribe()
    }
}

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda