1

I used the One component For many page in angular. I want to send a parameter from each component for show any different data. So I used the this code for call each component in Html:

<a  [routerLink]="['categories']" >Page1</a>
<a  [routerLink]="['categories']" >Page2</a>

Now in routes i Used this code

  {path:'Page1' ,component : ProductCategoriesPagesComponent ,data : {PageNo : 1}},
  {path:'Page2' ,component : ProductCategoriesPagesComponent,data : {PageNo : 2}}

Then I used this code in ProductCategoriesPagesComponent

ngOnInit() {
    this.route.data
        .subscribe(data => {
          console.log("data",data);
        });
}

But i get

data {}

In console. Please Help me how I get parameters from routes If I mistake, show the best way for this method. Thanks.

2 Answers 2

1

You need to inject ActivatedRoute and get the data from snapshot.

this.activatedRoute.snapshot.data['PageNo'];
Sign up to request clarification or add additional context in comments.

Comments

0

You can access the data by accessing the route's snapshot like the following:

this.route.snapshot.data['PageNo'];

So you can do the above instead of subscribing to the observable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.