Skip to content

Instantly share code, notes, and snippets.

@LidiaKovac
Last active August 21, 2023 07:42
Show Gist options
  • Save LidiaKovac/eb623b966e7f3c9033ad4787eb614053 to your computer and use it in GitHub Desktop.
Save LidiaKovac/eb623b966e7f3c9033ad4787eb614053 to your computer and use it in GitHub Desktop.
ROUTE PARAMETER // Angular
<a routerLink="/6">Portera' alla rotta /:id con params.get("id") uguale a 6</a>
this.router.navigate(['/', ID DOVE VOLETE NAVIGARE])
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
templateUrl: './details.component.html',
styleUrls: ['./details.component.scss']
})
export class DetailsComponent implements OnInit {
id!: string
constructor(private route: ActivatedRoute, private router: Router) {
}
ngOnInit() {
this.route.paramMap.subscribe(params => this.id = params.get("id")!)
//Dalla rotta correntemente attiva ci iscriviamo ai parametri e e salviamo nella variabile id li parametro in tempo reale.
//se non aveste chiamato il parametro "id", il parametro di .get() avra' il nome del parametro che avete scelto.
}
}
const routes: Routes = [{
path: ":id", //i due punti indicano un parametro DINAMICO. Ovviamente potete cambiare sia la rotta base che il nome del parametro,
//esempio: /profile/:userId, oppure /pippo/:paperino. L'importante e' che nei passaggi successivi utilizziate il nome corretto.
component: DetailsComponent
},
{
path: "",
component: HomeComponent
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment