Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
Angular Router pass data to component

Is it a good practice to pass data with the angular router to a component or should i use an service instead?

At the moment the component gets the data like this:

this.account = activatedRoute.snapshot.data.account
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There are several ways to pass data to an angular component.

For objects like user account, I would use a provider (to have it ready on component init), a service (for sharing around app) or a guard (e.g. if you want to navigate out when not logged in).

When I want to reuse the same component in different routes and give it some hints about is behavior, I would use router data.

Another use case I met is to define a global app state using the activated route(s). Each route may define its data, a service listen for router events and stores the merged state. It helps me with large apps to have a route-based configuration for title, metas, toolbar and menus visibility, etc...

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to pass data through a route, here is a simple example.

Make your route to look like this:

{ path: 'todo', component: TodoComponent, data: { id:'1', name:"Todo Title"} }

Then in your Component you can do something like this:

ngOnInit() {
      this.activatedroute.data.subscribe(data => {
          this.todo = data;
      })
}

Was this helpful?

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda