I'm using query params to navigate to a specific tab between components. The receiving component (which needs to check the query params) keeps returning the error:
Cannot set property active of undefined
In one component, I have the following html.
<button *ngIf="isLoggedInUser" style="position: absolute; right: 0" class="btn btn-link btn-sm"
[routerLink]="['/users/' + auth.currentUser.id + '/edit']" [queryParams]="{tab: 2}">
<i class="fas fa-pen"></i>
</button>
In the edit component (the one checking the query params), I have this in my ngOnInit using ActivatedRoute
.
this.router.queryParams.subscribe(params => {
const selectedTab = params["tab"];
console.log(this.editProfileTabs);
this.editProfileTabs.tabs[
selectedTab > 0 ? selectedTab : 0
].active = true;
});
When I try it...i get this in the console:
core.js:1673 ERROR TypeError: Cannot set property 'active' of undefined
any ideas? I would think the ternary would handle null/undefined stuff.
I've console logged the tabset and it does log tabs in that html group.
this.editProfileTabs
, this probably doesn't have values the index you are trying to fetch. – Ashish RanjanqueryParams
subscription, try logging like:console.log(JSON.parse(JSON.stringify(this.editProfileTabs)))
. – Ashish RanjansetTimeout()
calls change detection for the whole app while adetectChanges()
will call only for the current component and its children. – Ashish Ranjan