Alucor Rpa
Alucor Rpa

Reputation: 5

Access the routerlink data

I have a routerlink in an anchor tag, and 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.

Upvotes: 0

Views: 270

Answers (2)

Zerotwelve
Zerotwelve

Reputation: 2361

Route should work anyway like this:

constructor(private route: ActivatedRoute){}

ngOnInit() {
 const debug = this.route.snapshot.paramMap.get('debug');
}

Upvotes: 0

HassanMoin
HassanMoin

Reputation: 2194

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);
        });
    }
}

Upvotes: 2

Related Questions