henrychavez / nativescript-bottom-navigation

Nativescript plugin for Android & iOS to have the bottom navigation bar of Material Design
Apache License 2.0
58 stars 18 forks source link

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

Open ghost opened 5 years ago

ghost commented 5 years ago

The code of the demo on GitHub not working.

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

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

henrychavez commented 5 years ago

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 commented 5 years ago

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); }

ghost commented 5 years ago

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

henrychavez commented 5 years ago

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);
}
ghost commented 5 years ago

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

Thanks

henrychavez commented 5 years ago

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

ghost commented 5 years ago

yes, below the code:

<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>
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 commented 5 years ago

UPDATE:

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