Retrieving the name of the current page in ionViewCanEnter

While working with Ionic 2, I am currently facing a challenge in identifying the name of the page that triggered the navigation (PUSHER) before entering the destination page (PUSHEE).

Within the PUSHEE page, I have an ionViewCanEnter function where I need to determine the name of the PUSHER page. However, when I attempt to use NavController to retrieve the active page, it returns undefined. Although the NavController itself is not undefined, the response from getActive is. Can anyone recommend a solution for obtaining the name of the PUSHER page?

ionViewCanEnter(): boolean {
    console.log('active page', this.navCtrl.getActive());
    return true;
}

Answer №1

As far as I know, there is currently no straightforward method for retrieving the name of the active page. However, you can pass a string parameter through PUSHER like this:

this.navCtrl.push(HomePage, {pusherName: "MyPageName"});

Then, in the PUSHEE component:

ionViewCanEnter(): boolean {
    console.log('active page', this.navParams.get(pusherName));
    return true;
}

Edit: It's worth mentioning that you can obtain the class name (and consequently the page name) using the following code:

(this).constructor.toString()

In my testing, however, the constructor's name may be removed during ionic --prod execution. So, it's important to verify any alternative methods under --prod conditions.

Answer №2

I successfully navigated back to the previous page by utilizing the getViews method.

ionViewCanEnter(): boolean {
    console.log('getViews', this.navCtrl.getViews());
    var all_views = this.navCtrl.getViews();
    var last_view = all_views[all_views.length-1];
    var last_view_name = last_view.name;
}

Answer №3

Here is a straightforward example demonstrating how to redirect to another page:

<button ion-button  (click)="goToHomePage();">Home Page</button>

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HomePage } from '../home/home';

@IonicPage()
@Component({
  selector: 'page-level',
  templateUrl: 'level.html',
})
export class LevelPage {

  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

goToHomePage() {
 this.navCtrl.push(HomePage);
}

}

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

What is the best way to create a TypeScript function that merges actions together?

I am currently working on a TypeScript function similar to the following: import multipleActionObject from page; it("should be able to perform multiple operations", () => { multipleActionObject.chooseIndex(4).setValue(10); } Inste ...

Differentiate the array type within an object in TypeScript

I understand how to specify the type for a variable. let members: string[] = [] // this example works flawlessly My goal is to have an array of strings within an object. How can I structure it correctly? const team = { name: String, members<st ...

Tips for verifying that parameters possess designated characteristics in TypeScript

How can I ensure that data2 and data3 work correctly, while data1 triggers an error if the data type is not specified as shown in the code below? I need to validate that when a user enters params like {name: 'aa', age: 20}, it should throw an er ...

How come I am unable to fetch classes and enums from a namespace?

When using Typescript with pg-promise, I am facing an issue where I can't import the classes and enums as I normally would. Typically, when working with a library, I import a type, use it, and everything functions properly. However, in the snippet bel ...

Issue encountered while importing supercluster in TypeScript (TypeError: Unable to assign property 'options' to an undefined variable)

After attempting to import supercluster in TypeScript, I encountered the following error. Any helpful insights would be appreciated. ExceptionsManager.js:86 TypeError: Cannot set property 'options' of undefined This error is located at: ...

Tips for assigning an ID to a delete button when utilizing setValue in Angular 6

When we use setValue, how can we assign the ID of a row to the delete button? for (let i = 0; i < this.education.length; i++) { if (i !== 0) { const control = <FormArray>this.editEducation.controls['educationArray']; ...

Customizing the Global Error Handler in Angular

Our project utilizes a global error handler to manage errors. However, we have encountered a unique scenario where we need to handle a specific case separately but are unable to do so. The global error handler is configured in the app.module.ts: { provide: ...

Different ways to update the AutoComplete Style attribute

MuiInput-formControl { margin-top: 16px; Is there a way to reset the marginTop property to zero? I attempted the following method, but it was not successful. MuiFormControlLabel: { marginTop: 0, }, <Autocomplete disabl ...

Declaring scoped runtime interfaces with Typescript

I need to create a global interface that can be accessed at runtime under a specific name. /** Here is my code that will be injected */ // import Vue from "vue"; <- having two vue instances may cause issues // ts-ignore <- Vue is only ava ...

Angular4: Sharing breadcrumb view components via the router's `data` attribute

Currently in my Angular app (Angular v4), I'm working on developing a breadcrumb module. After coming across this insightful question on Stack Overflow, I learned about storing breadcrumb information in the router's data property as shown below: ...

In Angular 10, the custom CSS URL is loading after the standard styles.css file

Below is the configuration from my angular.json file: "options": { "outputPath": "dist/example", "index": "src/index.html", "main": "src/main.ts", ...

Import resolves Uncaught ReferenceError by preventing access to 'xx' before it is initialized

Currently, I am troubleshooting a peculiar error that has come up. Within my service file where all other services are stored, I have included the import of one component along with all the other services required by the frontend. import { VacationComponen ...

Angular 2: Enhancing Tables

I am looking to create a custom table using Angular 2. Here is the desired layout of the table: https://i.sstatic.net/6Mrtf.png I have a Component that provides me with data export class ResultsComponent implements OnInit { public items: any; ngO ...

Is Drizzle ORM able to handle decimal values in MySQL as strings?

The data structure: export const myTable = mysqlTable( "MyTable", { id: varchar("id", { length: 191 }).notNull(), value: decimal("value", { precision: 7, scale: 4 }).notNull(), createdAt: datetime("created ...

Excessive repetition in the style of writing for a function

When it comes to TypeScript, a basic example of a function looks like this: let myAdd: (x: number, y: number) => number = function ( x: number, y: number ): number { return x + y; }; Why is there redundancy in this code? I'm having trouble g ...

step-by-step guide to leveraging the heatmap functionality of google maps with angular agm

For my current Angular 5 project, I am utilizing the angular component @agm/core found at https://github.com/SebastianM/angular-google-maps to display Google Maps. It's been working well so far, but now I'm looking to incorporate a heatmap layer ...

Tips for deploying an Angular 2+ application on the OpenShift platform

I successfully developed an Angular application on my Windows PC, and now I am facing challenges in deploying it on Red Hat OpenShift. Despite searching for guides online, I have not been able to find helpful resources. If anyone has experience with deploy ...

Sorting complex strings in Typescript based on the dates contained within them

How can I sort a list of 2 strings with dates inside them so that the earlier one always comes first? The date is always after the second comma. For example, const example = ["AAA,5,2020-09-17T21:14:09.0545516Z", "AAA,0,2020-09-03T20:38:08. ...

Locate and modify a single item in a list using NGRX

I have an array of items stored in my ngrx/store. When the user clicks on a button, I need to retrieve a specific item based on its id and update its properties without using ngxr/entities. I have managed to achieve this functionality in my current imple ...

"Embracing the power of multiple inheritance with Types

I am struggling with the concept of multiple inheritance in TypeScript. It doesn't make sense to overload a hierarchy with too much functionality. I have a base class and several branches in the hierarchy. However, I need to utilize mixins to isolate ...