0

I have a service where I read couple of parameters from the URL. looks good till I try to inject the service in the constructor of the app.component.ts or I try to call a service method from the app.component.ts. Then the url-parameters are undefined.

Any idea what could be the reason for that?

my-service.service.ts:

...
public param1;
constructor(public router: Router, private http: HttpClient) {
    this.param1 = 
     this.router.parseUrl(this.router.url).queryParams['todo'];
    console.log('param1: ' + this.param1); // returns undefined if the service is injected in app.component
}
....

app.component.ts:

...
import { MyService } from './service/my-service.service';
....
constructor(private http: HttpClient, public myService: MyService) {
    ...
}
k.vincent
  • 3,743
  • 8
  • 37
  • 74
  • What if you try to parse the URL in the `ngOnInit` hook ? –  Mar 21 '18 at 07:33
  • It looks like https://stackoverflow.com/questions/49370474/angular-activated-route-paramsmap-empty-depending-on-where-i-specify-the-service – Eliseo Mar 21 '18 at 07:33
  • @trichetriche: `ngOnInit()` in `app.component.ts`? I did give it a try, but din't work. The issue is exactly when I inject the service in the in `app.component.ts` constructor. – k.vincent Mar 21 '18 at 07:43
  • No, in the `ngOninit` of the service. –  Mar 21 '18 at 07:44
  • @trichetriche: unfortunately it doesn't help... the service doesn't even fire `ngOnInit()`, so therefore I even can't `console.log('param1: ' + this.param1)`. Nothing happens in other words. – k.vincent Mar 21 '18 at 07:52
  • What do you mean it doesn't fire ? Maybe that's your issue ! –  Mar 21 '18 at 07:52
  • Here is what I tried in the service: `ngOnInit() {this.param1 = this.router.parseUrl(this.router.url).queryParams['todo']; console.log('param1: ' + this.param1);}`. nothing comes out in chrome console then. – k.vincent Mar 21 '18 at 07:55
  • @Eliseo: Yes, I'am using `canActivte` for some routes which I want to protect. But How can it be related to it? – k.vincent Mar 21 '18 at 07:58
  • Change 'public myService: MyService' to 'private myService: MyService'. Only then Angular directly instantiates it. –  Mar 21 '18 at 07:59
  • Sorry, I din't read your question :(. I think that you can try to use Location.path https://angular.io/api/common/Location#path , not router) – Eliseo Mar 21 '18 at 08:03
  • @DiabolicWords: no way.. always returns undefined even when using it as `private` – k.vincent Mar 21 '18 at 08:04
  • You say that you get `undefined` when the service is injected in `AppComponent` ... when do you NOT get `undefined`? Are you injecting it elsewhere? – Oscar Paz Mar 21 '18 at 08:36

1 Answers1

0

When we pass some params we always do it this way:

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

  constructor(
    private router: Router,
    private route: ActivatedRoute
  ){ 
      this.route.params.subscribe(params => {
        let todo = params['todo'];
      });
  }

Maybe you have to move the subscription to ngOnInit() but normally this should work.