Having trouble retrieving the Ionic 2 slides instance - getting a result of undefined

As I attempt to utilize the slides and access the instance in order to use the slideto functionality programmatically, I find myself encountering the issue of receiving 'undefined' back despite following the documentation.

Here is my TypeScript code:

import { Component, ViewChild } from '@angular/core';

import { NavController, NavParams, Slides} from 'ionic-angular';

@Component({
  selector: 'holiday',
  templateUrl: 'holiday.html'
})
export class Holiday {

    selectedItem: any;

    @ViewChild('ghbslides') slider: Slides;
    
    constructor(public navCtrl: NavController, public navParams: NavParams) {
        console.log(this.slider); // returns undefined
        this.selectedItem = navParams.get('holiday');
    } 

    onSlideChanged(slide) {
        console.log("slid");
    }

}  

Below is a snippet from the template where the slides are defined:

<ion-slides #ghbslides (ionDidChange)="onSlideChanged(index)">

      <ion-slide>
        <h2>Slide 1</h2>
      </ion-slide>

      <ion-slide>
        <h2>Slide 2</h2>
      </ion-slide>

      <ion-slide>
        <h2>Slide 3</h2>
      </ion-slide>

    </ion-slides>

I have attempted various solutions, but `this.slider` continues to be undefined. Any assistance would be highly appreciated as I might be overlooking something obvious.

Answer №1

One possible reason for this issue is that the page might not have finished loading when the constructor is executed. To resolve this, you can define the slider after the view has been fully loaded by overriding the ionViewDidLoad method.

ionViewDidLoad() {
        console.log(this.slider);
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Arrange items in a single parent Div in flex-column format, aligning them to the left and

In my Angular application, I am experimenting with stacking Bootstrap toast elements in a customized vertical layout. Specifically, I want certain toast elements to be positioned on the left side of the page (using the align-items-start style) and others o ...

Toggling multiple ions simultaneously does not function independently

I encountered a problem while working on an ionic app. I needed to have individual control over toggle switches, but my current code toggles all switches at once whenever one switch is tapped. Is there a way to fix this and manage each toggle switch separa ...

Angular 2: Embracing the Power of Hierarchical Selection

My goal is to create cascading selects where each option in a dropdown menu can lead to its own set of unique child options. This will result in a hierarchical structure of selectable items. To accomplish this, I have defined a class named fieldSelect tha ...

When incorporating leaflet-routing-machine with Angular 7, Nominatim seems to be inaccessible

Greetings! As a first-time user of the Leafletjs library with Angular 7 (TypeScript), I encountered an error while using Leaflet routing machine. Here is the code snippet that caused the issue. Any ideas on how to resolve this problem? component.ts : L. ...

Tips for maintaining the active state of an item within a component that loops through a dataset

I am working with an array of objects (specifically, posts represented as strings) and I am looking to be able to edit each one individually. However, I am encountering an issue where clicking on the edit button triggers editing for all posts at once: co ...

What is the process for displaying an enum in an Angular Mat Form Field?

Hey there! Check out my awesome app - My app. I need help with writing a Java/Visual Studio script for my app. How can I make it so that when I click on "Upgrade" for the id 1, the "Account" row displays CASH as the default value? Right now, no value is sh ...

In Typescript, the module source is imported rather than the compilation output

I created a custom module for personal use and decided to host it on a private GitHub repository. Within the module, I have included a postinstall script that runs: tsc -d -p .. Currently, the generated .js and .d.ts files are located alongside the source ...

What are the advantages of using interfaces in React?

Exploring Typescript's interface and its application in React has been an interesting journey for me. It seems that interfaces are used to define specific props that can be passed, acting as a form of protection against passing irrelevant props. My qu ...

Leveraging the @Input decorator to send external data into the Angular component

In my Ionic app, I am utilizing an Angular component. Within this component, there is a variable called headerText that is supposed to be initialized from the page where the component is being used. The issue arises when the headerText variable is always ...

Testing the receiveMessage function in SQS using Jest unit tests

Struggling to find the right approach for unit testing this function. I almost have it, but can't quite nail it down. Take a look at the function below: receiveMessage(callback: Function): any { this.sqs.receiveMessage( this.params, ...

Move after a specified amount of time

I'm struggling to implement a 6-second redirect in my Angular application that will take users to the homepage. The only resources I've found on this topic are for AngularJS. --------------------UPDATE--------------- Here are my current routes: ...

Unable to find solutions for all parameters in the Application Module: (?). Error occurred at syntaxError (compiler.js:1021) on the site stackoverflow.com

Upon reverting my Angular project from version 7 to 6 and integrating Angular Universal using @ngToolkit, a specific error message appeared in the browser console: The parameters for Application Module cannot be resolved: (?). at syntaxError (compiler.js: ...

Error encountered during sequelize synchronization: SQL syntax issue detected near the specified number

Four Sequelize Models were created using sequelize.define();. Each model is similar but with different table names. To avoid manual creation of tables in MySQL cli, the decision was made to use sequelize.sync() in the main index.js file to allow Sequelize ...

When using TypeScript, default imports can only be done with the 'esModuleInterop' flag enabled

Below is my current code snippet in index.ts: import { User } from "./datatypes" import express from 'express'; console.log("hello world") Displayed is the content of my package.json: { "name": "simple_app& ...

Angular Redirect Function: An Overview

In the Angular project I'm working on, there is a function that should navigate to the home when executed. Within this function, there is a condition where if true, it should redirect somewhere. if (condition) { location.url('/home') ...

Angular 10: How to Retrieve NgForm from Projected Content

Imagine I have a component structured like this: <app-my-typical-form> <app-child-in-content></app-child-in-content> </app-my-typical-form> where the code for my-typical-form.component.html is as follows: <form #f="ngForm"&g ...

Adjusting the IntelliSense Functionality in Monaco Editor

Currently, I am testing out the CompletionItemProvider feature for my project. I have implemented two separate CompletionItemProviders. One is set to trigger when any alphabet letter is typed and the other triggers when the user enters a single quote chara ...

Trouble with Nextjs link not functioning properly with a URL object when incorporating element id into the pathname

Recently I added translations to my website, which means I now need to use a URL object when creating links. Everything has been going smoothly with this change except for one issue: when I try to click a link that points to /#contact. When simply using h ...

Tips for showing data from an hour ago in Angular

Here is the code snippet provided: data = [ { 'name' : 'sample' 'date' : '2020-02-18 13:50:01' }, { 'name' : 'sample' 'date' : '2020-02- ...

Looking for assistance with an Angular2 post request?

I'm having an issue with a post request to obtain a token granting access to another access token. Each time I attempt to make the post request, I encounter an error stating that the access_token property is trying to read something undefined. It seem ...