nativescript-bottom-navigation icon indicating copy to clipboard operation
nativescript-bottom-navigation copied to clipboard

The methods ('selectTab'/'showBadge") not working

Open ghost opened this issue 5 years ago • 8 comments

The code of the demo on GitHub not working.

@ViewChild('bottomNavigationBar', { read: ElementRef, static: false }) private _bottomNavigationBar: ElementRef<BottomNavigationBar>;

onBottomNavigationBarLoaded(): void { const bottomNavigationBar = this._bottomNavigationBar.nativeElement; bottomNavigationBar.selectTab(this.tabSelectedIndex); // bottomNavigationBar.showBadge(2, 4); }

return 'ERROR TypeError: Cannot read property 'nativeElement' of undefined'

Can I solve this? Thanks

ghost avatar Oct 08 '19 11:10 ghost

When you use view child you should use ngafteeviewinit life cycle method, if you use the bottomNavigationBarLoaded method this method gets an argument with an object that in this will be your native element

henrychavez avatar Oct 08 '19 18:10 henrychavez

In the xml Put the $event arg like this (loaded)="onbottomNavigationBarLoaded($event)"

Then you need to change your function in the ts file like this onbottomNavigationBarLoaded(args: EventData) { const bar = args.object; bar.showBadge(2,4); }

henrychavez avatar Oct 08 '19 19:10 henrychavez

Thanks. I try your solution but the following error is returned: "Property 'showBadge' does not exist on type 'Observable'."

How can i fix it? Thanks

ghost avatar Oct 09 '19 12:10 ghost

yeah, I forgot to mention, the EventData has an object that is type Observable, in this case, you need to cast it as a BottomNavigationBar class like this

onbottomNavigationBarLoaded(args: EventData) {
  const bar = args.object as BottomNavigationBar;
  bar.showBadge(2,4);
}

henrychavez avatar Oct 09 '19 15:10 henrychavez

Now there's no error, but when I use 'bar.showBadge(2,4);' no badge is shown.

Thanks

ghost avatar Oct 10 '19 08:10 ghost

can you show me the code you are working on? is the same as the demo? or did you made some changes? because I just run it and everything is ok

henrychavez avatar Oct 10 '19 17:10 henrychavez

yes, below the code:

  • in .html:
<BottomNavigationBar
        activeColor="#00B8A9"
        inactiveColor="#BCBCBC"
        backgroundColor="#FDFFFC"
        (tabSelected)="onBottomNavigationTabSelected($event)"
        (loaded)="onBottomNavigationBarLoaded($event)"
>
    <BottomNavigationTab title="Home" icon="res://ic_home"></BottomNavigationTab>
    <BottomNavigationTab title="Amici" icon="res://ic_friends"></BottomNavigationTab>
    <BottomNavigationTab title="Notifiche" icon="res://ic_notification"></BottomNavigationTab>
    <BottomNavigationTab title="Profilo" icon="res://ic_profile"></BottomNavigationTab>
</BottomNavigationBar>
  • in .ts:
import { TabPressedEventData, TabSelectedEventData, BottomNavigationBar } from "nativescript-bottom-navigation";

onBottomNavigationBarLoaded(args: EventData): void {
        const bar = args.object as BottomNavigationBar;
        bar.showBadge(1,1);
        // bar.selectTab(2);
    }

ghost avatar Oct 11 '19 07:10 ghost

UPDATE:

The "selectTab" works, but no "showBadge".

ghost avatar Oct 16 '19 09:10 ghost