How to stop ActivatedRoute.queryParams.subscribe to be called on back navigation?
How to stop ActivatedRoute.queryParams.subscribe to be called on back navigation?
platform(s) the issue occur on
- iOS
- iOS 11
- ipad mini2 , iphone 6 plus, Nativescript playground
the following version numbers:
tns-core-moduels 4.2.0 tns-ios 4.2.0 angular/compiler-cli 6.1.2 angular/router 6.1.2 nativescripte-angular 6.1.0
the steps to reproduce it:
There are 3 page: home, entity, contact. they are root level page and using page-router-outlet
- In home page, go to entity page with param id = 1
this.routerExtensions.navigate(['/entity'],
{
queryParams: {
id: '1'
}
}
);
- In entity page, go to contact page with param id = 2
this.routerExtensions.navigate(['/contact'],
{
queryParams: {
id: '2'
}
}
);
- In contact page, tap the system back button on top-left to go back to entity page
expect result: In entity page, router.queryParams.subscribe should NOT be called on back navigation and show entity page without any refresh.
export class EntityComponent implements OnInit {
...
ngOnInit(): void {
//if it's from Home page, that's OK; but how to stop it to be called on navigating back?
this.router.queryParams.subscribe(params => {
console.log("EntityComponent queryParams with id : " + params.id);
//To refresh page on regular navigating using id = 1.
//On back navigation, the page shouldn't be refreshed.
});
}
actual result: this.router.queryParams.subscribe is called with param id= 2 ( the contact page id)
code : https://play.nativescript.org/?template=play-ng&id=SgrzAJ&v=9
Hi @anorthwolf,
I would suggest unsubscribing from the this.router.queryParams when you navigate to another page and to subscribe for it again when you are entering the EntityComponent. For example:
entity.component.ts
onButtonTap(): void {
this.router.queryParams.unsubscribe();
console.log("Entity Button was pressed, go to contact Page with id = 2");
this.routerExtensions.navigate(['/contact'],
{
queryParams: {
id: '2'
}
}
);
}
For further questions, I would suggest using this StackOverflow thread. This repository is used for bug reporting and feature request.
hi, @tsonevn ,
this.router.queryParams.unsubscribe();
has compile problems in the playground. this.router.queryParams doesn't have the 'unsubscribe' method. I've moved the question to here
Thanks
Hi @anorthwolf, I have researched your case further and found this thread in StackOverflow. You can try to unsubscribe as follows:
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { RouterExtensions } from "nativescript-angular/router";
@Component({
selector: "Entity",
moduleId: module.id,
templateUrl: "./entity.component.html",
styleUrls: ['./entity.component.css']
})
export class EntityComponent implements OnInit {
private subs;
constructor(private routerExtensions: RouterExtensions, private router: ActivatedRoute) {
}
ngOnInit(): void {
console.log('EntityComponent ngOnInit');
//if it's from Home page, that's OK; but how to stop it to be called on back navigation?
this.subs=this.router.queryParams.subscribe(params => {
console.log("EntityComponent queryParams with id : " + params.id);
//To refresh page using id = 1. But On back navigation, the page shouldn't be refreshed.
});
}
onButtonTap(): void {
this.subs.unsubscribe();
console.log("Entity Button was pressed, go to contact Page with id = 2");
this.routerExtensions.navigate(['/contact'],
{
queryParams: {
id: '2'
}
}
);
}
Hope this helps.
hi, @tsonevn , this solution works. Thanks for your help. I've posted my implement on StackOverflow