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

1.1K
Visualizações
Angular 7/8 - How to get url parameters in app component

I have Single sign on in place but for testing I want to read the values from the url localhost:4200/?id=test&name=testing&email=testing@test.com and pass them to an API in app component.

there will be a flag on which basis I will reading from url instead of using single sign on function

if (url_enabled == true) {
    getParamsFromUrl()
 } else {
   singleSignOn()
 }

I tried ActivatedRoute but it doesn't seem to be working.

I have tried queryParams, params, url, queryParamsMap but none of these seems to be working. all I get is empty value.

inside app component

app.component.ts

 getParamsFromUrl() {
    this._router.events.subscribe((e) => {
      if (e instanceof NavigationEnd) {
        console.log(e.url)
      }
    })
  }

 this.route.queryParams.subscribe(params => {
      console.log(params);
    })

app.component.html

<router-outlet></router-outlet>

app-routing.module.ts

const routes: Routes = [
  {path:'*/:id', component: AppComponent},
];

I have tried whatever I could found on stackoverflow or other blogs. Can somebody point out what am I missing here?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

For this route: You can try this way:

const routes: Routes = [
   {path:'*/:id', component: AppComponent},
];   

In AppComponent .ts file:

    constructor(
      private activatedRoute: ActivatedRoute,
    ) { }




    ngOnInit() {
      this.activatedRoute.params.subscribe(params => {
          const id = params['id'];
          console.log('Url Id: ',id);
    }

    OR

    ngOnInit() {
      this.activatedRoute.queryParams.subscribe(params => {
          const id = +params.id;
          if (id && id > 0) {
           console.log(id);
          }
      });
    }
  
  
over 4 years ago · Santiago Trujillo Relatório

0

first of all there is an url with queryParams like yours :

localhost:4200/?id=test&name=testing&email=testing@test.com

in this way tou get to the queryparams with ActivatedRoute object lik :

 this.name = this.activatedRoute.snapshot.queryParamMap.get('name'); // this.name = 'testing'

Or :

 this.activatedRoute.queryParams.subscribe(params => {
       this.name= params['name'];
     });

and the other way is

localhost:4200/test/testing/testing@test.com

you use for sync retrieval (one time) :

this.name = this.activatedRoute.snapshot.ParamMap.get('name');
over 4 years ago · Santiago Trujillo Relatório

0

Angular comes us with the ActivatedRoute object. We can access the URL parameter value in same way its done above with little difference. Data in this type can be accessed with two different ways. One is through route.snapshot.paramMap and the other is through route.paramMap.subscribe. The main difference between the two is that the subscription will continue to update as the parameter changes for that specific route.

ngOnInit() {
    this.route.paramMap.subscribe(params => {
        this.userType = params.get("userType")
    })
}
over 4 years ago · Santiago Trujillo Relatório
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