question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Ionic v4] Bug: Viewchild not binding properly.

See original GitHub issue

Bug Report

Ionic Info Run ionic info from a terminal/cmd prompt and paste the output below.

Ionic:

   ionic (Ionic CLI)          : 4.0.6
   Ionic Framework            : @ionic/angular 4.0.0-beta.2
   @angular-devkit/core       : 0.7.3
   @angular-devkit/schematics : 0.7.3
   @angular/cli               : 6.1.3
   @ionic/ng-toolkit          : 1.0.6
   @ionic/schematics-angular  : 1.0.4

Cordova:

   cordova (Cordova CLI) : 8.0.0
   Cordova Platforms     : android 7.0.0, ios 4.5.5

System:

   Android SDK Tools : 25.2.5
   ios-deploy        : 1.9.2
   ios-sim           : 6.1.2
   NodeJS            : v8.9.4 (/Users/dtaalbers/.nvm/versions/node/v8.9.4/bin/node)
   npm               : 5.6.0
   OS                : macOS High Sierra
   Xcode             : Xcode 9.4.1 Build version 9F2000

Environment:

   ANDROID_HOME : /usr/local/Cellar/android-sdk/24.4.1_1

Describe the Bug I can’t get a reference to the tabbar from my viewmodel with the ViewChild. It’s not undefined but I get a object without the events/properties described in the docs.

Related Code My view

<ion-tabs #tabs [tabbarHidden]="!isAuthenticated" color="primary">
    <ion-tab label="Home" icon="home" href="/tabs/(home:home)">
        <ion-router-outlet name="home"></ion-router-outlet>
    </ion-tab>
    <ion-tab label="workspace" icon="home" href="/tabs/(workspace:workspace)">
        <ion-router-outlet name="workspace"></ion-router-outlet>
    </ion-tab>
    <ion-tab label="login" icon="home" href="/tabs/(login:login)">
        <ion-router-outlet name="login"></ion-router-outlet>
    </ion-tab>
</ion-tabs>

My tabs viewmodel.

export class TabsPage implements OnInit, OnDestroy, AfterViewInit {

    @ViewChild('tabs') private tabs: Tabs;

    private isAuthenticated: boolean;
    private subscription: Subscription;

    constructor(
        private navController: NavController,
        private authService: AuthenticationService
    ) { }

    public async ngOnInit(): Promise<void> {
        this.isAuthenticated = await this.authService.isAuthenticated();
        console.log(this.tabs);
        this.subscription = this.tabs.ionChange.subscribe(async (x) => {
            this.isAuthenticated = await this.authService.isAuthenticated();
        });
    }

    public async ngAfterViewInit(): Promise<void> {
         console.log(this.tabs);
    }

    public async ngOnDestroy(): Promise<void> {
        this.subscription.unsubscribe();
    }
}

The only thing I am getting from the console.log(this.tabs) is this:

30|690x39

and of course the error

Error: Uncaught (in promise): TypeError: Cannot read property 'subscribe' of undefined
TypeError: Cannot read property 'subscribe' of undefined

Expected Behavior I expect the ViewChild binding the correct object with the correct methods and parameters. That way I can subscribe to the tab change events to show the tabbar when the user is authenticated.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
brandyscarneycommented, Aug 15, 2018

@dtaalbers Awesome, I’m glad that workaround works! We are looking into how we can remove the need for {read: Tabs} though.

That issue seems to be unrelated. I tested out ionChange using the following:

<ion-tabs #tabs (ionChange)="tabsChanged($event)">

and it is working. So it seems to only be the subscribe that isn’t working. It would be great if you could create a new issue. Please feel free to tag me in it. 🙂

0reactions
ionitron-bot[bot]commented, Sep 14, 2018

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Ionic v4] Bug: Viewchild not binding properly. #15176 - GitHub
Bug Report Ionic Info Run ionic info from a terminal/cmd prompt and paste the output below. Ionic: ionic (Ionic CLI) : 4.0.6 Ionic...
Read more >
content is undefined and I can't scroll to bottom - ionic-v3
Hi, I've just detected that sometimes when I add rows to my list and try to scroll to bottom using: //@ViewChild(Content) content: Content; ......
Read more >
Angular 2 @ViewChild annotation returns undefined
The issue as previously mentioned is the ngIf which is causing the view to be undefined. The answer is to use ViewChildren instead...
Read more >
Angular ng-content and Content Projection: A Complete Guide ...
One of the Angular features that help us the most in building reusable components is Content Projection and ng-content .
Read more >
How To Use ViewChild in Angular to Access a Child ...
There may be situations where you want to access a directive, child component, or a DOM element from a parent component class. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found