anguarl queryParams не получает параметры - PullRequest
0 голосов
/ 30 января 2020

navbar.component. html

<a *ngFor="let item of studentClasses" routerLink="/classes/all-courses/, {queryParams: {prop: item.className}}">

class-routing.module.ts

export const routes: Routes = [
  {
    path: 'all-courses/:className',
    component: AllCoursesComponent
  },
]

all-courses.component.ts

ngOnInit() {
    this.activeRoute.queryParams.subscribe(queryParam => {
      console.log(queryParam);
      // here I'm receving empty object. But I'm expecting to receive className
    });
}

Ответы [ 2 ]

0 голосов
/ 30 января 2020

импортируйте модуль ActivatdeRoute и Router в свой компонент и вставьте их в конструктор

import { ActivatedRoute, Router } from '@angular/router'

contructor(private route: ActivatedRoute, private router: Router){ ... }
Subscribe the ActivateRoute to the ngOnInit

ngOnInit() {

    this.route.queryParams.subscribe(params => {
      console.log(params);

    })
}
0 голосов
/ 30 января 2020
You can set query params and fragment as follows:

`<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}">
  link to user component
</a>`


RouterLink will use these to generate this link: 

`/user/bob?debug=true`


  `constructor(private route: ActivatedRoute){}`

  `ngOnInit(){
      this.route.queryParamMap
                .map((params: Params) => params.params)
                .subscribe( (params) => {
                     if(params && params['test']){
                        let testQueryParamVal = params['test'];
                     }
                 });
  }`
...