0

I am trying to get my activatedRoute in my Angular app, so that I can load this within the onInit life cycle hook, so that when a user returns to a component, the component will load with the state of the url as they last left it.

I have already imported activatedRoute and am injecting it in the constructor:

constructor(private route: ActivatedRoute) {}

Then, in onOnit, just to see what I have, I tried this:

console.log('route info: ', this.route.queryParams);

What I get back is a behavior subject, so I try adding this:

console.log('route info: ', this.route.queryParams.toArray().subscribe());

This now returns a subscriber.

Bottom line, what I want is to be able to capture the current url/query string, and load it up within ngOnInit. Something like this:

ngOnInit() {
  this.loadData();
}

loadData() {
  this.route.queryParams;
}

Does this look close? How can I actually get the component to use activatedRoute to load up what's in the url/query string?

UPDATE: After a suggestion below, I'm thinking it could look something like this?

ngOnInit() {
    this.loadData();
}

loadData() {
    let currentRoute = this.route.params.subscribe(params => {
        // logic goes here
        console.log(params['firstName']);
        return params;
      });
    this.router.navigate([currentRoute]);
}

UPDATE 2:

If I do this:

ngOnInit() {
    this.activatedRoute.params.subscribe((params: Params) => {
        let myParams = params;
        console.log(myParams);
      });
}

I get this back in the console:

{pn_firstName.e: "1", pn_firstName.v: "John"}

And this is what my url/query string looks like:

http://localhost:4200/consulting;pn_firstName.e=1;pn_firstName.v=John

So it does seem to be getting the query string from what I can see in the console.

So, bottom line, will this load up the current state of the url/query string when the component loads?

ngOnInit() {
    let currentRoute = this.activatedRoute.params.subscribe((params: Params) => {
        let myParams = params;
        console.log(myParams);
        return myParams;
    });
    this.router.navigate([currentRoute]);
}

Apparently, not, because this is what I get for the url when loading the component:

http://localhost:4200/consulting;closed=false;_parent=null;_parents=null;_subscriptions=%5Bobject%20Object%5D;syncErrorValue=null;syncErrorThrown=false;syncErrorThrowable=false;isStopped=false;destination=%5Bobject%20Object%5D
Muirik
  • 6,049
  • 7
  • 58
  • 116

3 Answers3

1

If you are looking something to reload the current route without loosing the parameters you can try the below strategy

On your component constructor

constructor(private router:Router){
    this.router.routeReuseStrategy.shouldReuseRoute = function(){
            return false;
     } }

And reload to the same route you are currently in. From the URL property you will get the entire url

this.router.navigated = false;
      this.router.navigate([this.router.url]);

Another Approach

create a method

redirectTo(uri) {
    this.router.navigateByUrl('/', {skipLocationChange: true}).then(() =>
    this.router.navigate([uri]));
  }

And call the same like below

 this.redirectTo(this.router.url);
Jameel Moideen
  • 7,542
  • 12
  • 51
  • 79
  • Does the second part go within ngOnInit? – Muirik Sep 25 '18 at 20:59
  • @Muirik Yes. I will edit my answer with another approach also. Both will work – Jameel Moideen Sep 25 '18 at 21:02
  • You can test the same with setTimeOut . setTimeout(() => { this.redirectTo(this.router.url); }, 5000); – Jameel Moideen Sep 25 '18 at 21:06
  • Thanks, will give these a try in a little bit. The latter one looks preferable to me. – Muirik Sep 25 '18 at 21:24
  • So, I tried the second approach, and it appears to put the app is some sort of infinite loop. The page never loads fully. I defined `redirectTo(uri)` just as you described here, and then called `this.redirectTo(this.router.uri)` from within `ngOnInit()`. As an aside, when I run `console.log(this.router.url)` the correct url logs to the console. Is there some sort of security issue preventing the app from loading with this method? Some problem with how this relates to our routes? – Muirik Sep 26 '18 at 00:37
  • @Muirik If that not works for you check this link please http://tech-blog.maddyzone.com/angularjs/javascript/how-reload-current-route-angular2 – Jameel Moideen Sep 26 '18 at 02:51
0

Your loadData() can look like following:

this.route.params.subscribe(params => {
  // logic goes here
  console.log(params['id']); // example 
});
Peter Kim
  • 1,929
  • 14
  • 22
0

You can use snapshot for route

this.route.snapshot - if you don't wanna to use subscriber as this.route.params.subscribe

same for queryParams - it's accessible via snapshot

un.spike
  • 4,857
  • 2
  • 18
  • 38