Which page is open

#1

Hello,

I need to know which page is open when I have tabs.
I have the following situation:
I navigate from page that is from the tabs to page that isn’t. Than I open the same page through the sidebar.
How do I know that the page is already open and prevent it from opening?

For example from “Home” page, click on “Go to News page” button.
How to know that “News” page is open and to prevent it from opening form sidebar?
example

#2

This is quite simple, do the following:

checkpage(){
  let checkViews = this.navCtrl.getAllChildNavs();
  if(checkViews.component == News){
     console.log('News is already in the Navstack!);
  }else{
  this.navCtrl.push(News);
  }
}

This should do what you wanted :blush::four_leaf_clover:

For more info check out this link.

#3

Thanks,

It’s seams that checkViews returns an array.

However, if I get the element from there I have access to _componentName.

But in_componentName the value is “tabs” instead of “news”.

#4

Yes it returns the array of wich Pages you were on previosly and wich are still open in the background.

  if(checkViews.component == News){
     console.log('News is already in the Navstack!);
  }

Here we are looking if News is in that array if it is in the array it gives the console log. You can write any specific Page in there.


I don’t quite understand what you mean here:

However, if I get the element from there I have access to _componentName.
But in_componentName the value is “tabs” instead of “news”.

Do you mean that it returns something completly differnt ? :confused:

#5

By default, the app opens the “home” page which is in tabs.
In “home” page there is a link to “news” page.
But when I click on that button and open the “news” page in this.navCtrl.getAllChildNavs() it will only be “tabs” page

#6

So in conclusion you didn’t open News-Page yet because it only shows “tabs-Page” in the navStack. Nothing wrong here. Maybe I got your question wrong if so please explain exactly what you want to achieve since the code I’ve provided you is working fine.

#7

Sorry,
I’m going to explain what I mean by steps:

  1. Open the app with “home” page by default;
  2. Click on “Go to News page” button which is in “home” page;
  3. “News” page is opened (I’m still in “tabs” page);
  4. Open the sidebar and click on “News”;
  5. I want to prevent from opening the “News” page because it’s already opened;
#8

Ah so you want to prevent it when its already ACTIVE and not just in the navStack.

Change the code to this and it should work just fine:

checkpage(){
  let checkViews = this.navCtrl.getActive();
  if(checkViews.component == News){
     console.log('News is already in the Navstack, don't push!);
  }else{
  console.log('No Newspage open you can push!');
  }
}
pushToNews(){
  this.checkpage();
}

You can use pushToNews for your Button or you can also use checkpage for your Button its up to you. What this code does = it checks wich Page is currently open. If News is currently open it will give you ELSE if its not open it will give u IF.

1 Like