Reputation: 27
I have two parameterize routes
{ path: 'mails', component: MailsComponent, canActivate: [AuthGuard] },
{ path: 'mails/:label', component: MailsComponent, canActivate: [AuthGuard] },
{ path: 'mails/folder/:folder', component: MailsComponent, canActivate: [AuthGuard] }
In the component i want to get routes parameter based on condition .
ngOnInit(): void{
if (this.googleAuth.stateFlag) {
// labels
this.route.paramMap.subscribe(route => {
this.label$ = route.get('label');
this.googleAuth.selectedEmailLabel(this.label$);
});
}
else {
// folder
this.route.paramMap.subscribe(route => {
this.folder$ = route.get('folder');
console.log('folder handle:', this.folder$);
this.googleAuth.selectedEmailFolder(this.folder$);
});
}
}
Although there is condition, every time it executes else block.
Upvotes: 1
Views: 92
Reputation: 9260
The issue is likely because ngOnInit
is only called once in the component's life, and the component can be used multiple times on route changes. Try subscribing to the paramMap once, and handling logic within:
ngOnInit(): void{
this.route.paramMap.subscribe(route => {
if (this.googleAuth.stateFlag) {
this.label$ = route.get('label');
...
} else {
this.folder$ = route.get('folder');
...
}
});
}
Upvotes: 2