I have a routerlink in an anchor tag, I want to access the value passed. How can I do it? enter image description here
As you can see in the above screenshot, "bob" is the data is being passed to the component when this anchor tag is clicked, so how can I access this value in the component when it loads? Please note that it is a child component of another component therefore route is not used.
Thank you
CodePudding user response:
You can access the data using the Activated Route service of Angular by injecting it into the component you need data in. For example, the anchor is in Component A, and you navigate to Component B where you require the data Bob. You can inject the Acvtivated Route in Component B and get the param using it
constructor(private activatedRoute: ActivatedRoute){
ngOnInit(): void {
// For Params
this.activatedRoute.params.subscribe((params: Params) => {
// Should display Bob
console.log(params);
});
this.activatedRoute.queryParams.subscribe((queryParams: Params) => {
//Should display { debug: true} object
console.log(queryParams);
});
}
}
CodePudding user response:
Route should work anyway like this:
constructor(private route: ActivatedRoute){}
ngOnInit() {
const debug = this.route.snapshot.paramMap.get('debug');
}
