5

I'm new to Angular and I've looked for solution of my problem, but unfortunately I haven't found that. When I 'm passing parameters to the router (class Router), but list of params (on target site) is empty.

let navigationExtras: NavigationExtras ={
  queryParams: {
    uri: item.uri
  }
}

this.router.navigateByUrl('articlelist/article-details', navigationExtras);

Article-details component (route is ActivatedRoute class):

   ngOnInit(): void {
        console.log("ArticleDetailsComponent " + JSON.stringify(this.route.url));
    this.route.queryParamMap.map(paramMap =>{
            this.uri = paramMap.get('uri');
            console.log(this.uri);
        });
    }

Routing in ArticleList module (one of modules in my project):

const routes: Routes = [{
  path: '',
  component: ArticleListComponent,
    data: {
      title: 'ArticleList'
    },
  },
{
  path: 'article-details',
  component: ArticleDetailsComponent,
  data: {
    title: 'ArticleDetails'
  },
}
];

When I logged route.url in target component the result was:

ArticleDetailsComponent {"_isScalar":false,"observers":[],"closed":false,"isStopped":false,"hasError":false,"thrownError":null,"_value":[{"path":"article-details","parameters":{}}]}

If somebody knows how to fixed it, I'll be grateful for help :)

miclski
  • 53
  • 1
  • 1
  • 3

3 Answers3

6

You should need to use ActivatedRoute in order to get all query params.

Try this -

constructor(
    private activatedRoute: ActivatedRoute
  ) { }

this.activatedRoute.queryParams.subscribe(params => {
      const URI = params['uri'];
    });

Update

Try to send params like this -

this.router.navigate(['articlelist/article-details' ], {
      queryParams: {
        uri: item.uri || 'Default'
      }
    });
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
0

You can define the param in your app.routing.module.ts:

{ path: 'examplePath/:id', component: YourComponent},

and send like that:

this.router.navigate(['/examplePath/' + id]); (with @angular/router/Router)

Then in your component constructor you can get param (with @angular/router/ActivatedRoute):

this.activatedRoute.params.subscribe(params => {
    this.yourObject = params['id'];
}
Rence
  • 2,900
  • 2
  • 23
  • 40
chinaski
  • 116
  • 1
  • 7
-2

Instead of relying on Observables, I would advise you to get a snapshot of your route. This will give you a simple object containing your params.

constructor(private route: ActivatedRoute) {}

ngOnInit() {
  console.log(this.route.snapshot.queryParams);
}
  • 1
    Unfortunately, result is an empty list :/ – miclski Jun 26 '18 at 08:39
  • Then you have an issue with your routing. Please post a [mcve] reproducing the issue. –  Jun 26 '18 at 08:40
  • 3
    I also see an empty list. When I use the `this.activatedRoute.queryParams.subscribe` form, I do get the query parameters eventually, but if I use the `this.activatedRoute.snapshot.queryParams` form that's empty. It's like the snapshot is being taken too early. – Eric Lee Nov 02 '18 at 22:49