Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of queryParams is empty in Angular 5 routing

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 :)

like image 690
miclski Avatar asked Jun 26 '18 08:06

miclski


People also ask

What is path match full in Angular?

pathMatch: 'full' means, that the whole URL path needs to match and is consumed by the route matching algorithm.

What is queryParams in Angular?

The query parameters feature in Angular lets you pass the optional parameters while navigating from one route to another.

How to define route in Angular?

Define your routes in your Routes array. Each route in this array is a JavaScript object that contains two properties. The first property, path , defines the URL path for the route. The second property, component , defines the component Angular should use for the corresponding path.


1 Answers

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'
      }
    });
like image 186
Pardeep Jain Avatar answered Nov 02 '22 09:11

Pardeep Jain