xxxxxxxxxx
export class ComponentA {
// We need access to the Angular router object to navigate programatically
constructor(private router: Router){}
navigateWithArray(): void {
// Create our query parameters object
const queryParams: any = {};
// Create our array of values we want to pass as a query parameter
const arrayOfValues = ['a','b','c','d'];
// Add the array of values to the query parameter as a JSON string
queryParams.myArray = JSON.stringify(arrayOfVAlues);
// Create our 'NaviationExtras' object which is expected by the Angular Router
const navigationExtras: NavigationExtras = {
queryParams
};
// Navigate to component B
this.router.navigate(['/componentB'], navigationExtras);
}
}
xxxxxxxxxx
constructor(private router: Router) { }
public myMethodChangingQueryParams() {
const queryParams: Params = { myParam: 'myNewValue' };
this.router.navigate(
[],
{
relativeTo: activatedRoute,
queryParams: queryParams,
queryParamsHandling: 'merge', // remove to replace all query params by provided
});
}
xxxxxxxxxx
import { ActivatedRoute } from '@angular/router';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-example',
template: '' // Your component template here
})
export class ExampleComponent implements OnInit {
constructor(private route: ActivatedRoute) { }
ngOnInit(): void {
this.route.queryParams.subscribe(params => {
// Access query parameters here
console.log('Query Params:', params);
// Example usage: accessing a specific parameter
const paramValue = params['paramName'];
console.log('Param value:', paramValue);
});
}
}