简体   繁体   English

Ionic2 - 检查页面是否处于活动状态

[英]Ionic2 - check if page is active

How can I check if a ionic2 page is active?如何检查 ionic2 页面是否处于活动状态?

For example I want to hide a button if a page is active:例如,如果页面处于活动状态,我想隐藏一个按钮:

<button primary [hidden]="isActive('actualPageName')">
     Should be hidden
</button>

IONIC 2 & 3: Because of the uglification you cannot use pagename for production. IONIC 2 & 3:由于丑化,您不能使用 pagename 进行生产。

I check the active page by looking if the last page on the controller stack is of the instance of the page:我通过查看控制器堆栈上的最后一页是否属于该页面的实例来检查活动页面:

import { NavController } from 'ionic-angular';
import { myPage } from '../../pages/my/my';
...
constructor(public navCtrl: NavController) {}
...
let active = this.navCtrl.last().instance instanceof MyPage;

IONIC 4: Routing is changed.离子 4:路由已更改。 This looks like the new way:这看起来像新的方式:

import { Router } from '@angular/router';
...
constructor(public router: Router) {}
...
let active =  this.router.isActive('mypage', false)

You can retrieve the active page and check its name:您可以检索活动页面并检查其名称:

public isActive(pageName: string): boolean {
   return this.navCtrl.getActive().name === pageName);
}

UPDATE更新

In the comments you can see some users that claim this solution is not working for them due to the Uglify process.在评论中,您可以看到一些用户声称由于 Uglify 过程,此解决方案对他们不起作用。 It sounds reasonable, but I still found the solution seems to work.这听起来很合理,但我仍然发现解决方案似乎有效。 I've compiled an APK with this code using this command:我已经使用以下命令使用此代码编译了一个 APK:

ionic cordova build android --prod --release

During compilation you can see:在编译过程中,您可以看到:

...
[13:00:41]  uglifyjs started ... 
[13:00:43]  sass finished in 2.45 s 
[13:00:43]  cleancss started ... 
[13:00:46]  cleancss finished in 3.05 s 
[13:00:57]  uglifyjs finished in 16.27 s
...

Then, when I run that app in the Android emulator I got the right page name using this.navCtrl.getActive().name .然后,当我在 Android 模拟器中运行该应用程序时,我使用this.navCtrl.getActive().name获得了正确的页面this.navCtrl.getActive().name

I must say I've not tested it with a signed application in a real device.我必须说我还没有在真实设备上用签名的应用程序测试过它。

May be Android or the emulator is not affected, may be the problem described by those users is solved in recent releases, may be I'm doing something wrong.可能是 Android 或模拟器不受影响,可能是那些用户描述的问题在最近的版本中得到了解决,可能是我做错了什么。 Because I don't know the response to this questions I maintain my answer but I've added this info.因为我不知道对这些问题的回答,所以我保留了我的答案,但我已经添加了这些信息。 Please, if you know more about this comment it instead of just ignoring or donwvoting this answer.请,如果您对此评论有更多了解,请不要忽略或不投票此答案。 If this answer is proven wrong I'll happily modify or delete it.如果这个答案被证明是错误的,我会很乐意修改或删除它。

Update to the @sanzante answers.更新@sanzante 答案。 You can retrieve an active page and check it's name as follows.您可以检索活动页面并检查其名称,如下所示。

public isActive(pageName: string): boolean {
   return this.navCtrl.getActive().id === pageName);
}

It will work for the --prod build version as well.它也适用于 --prod 构建版本。

ionic cordova build android --prod --release离子科尔多瓦构建 android --prod --release

http://ionicframework.com/docs/v2/api/navigation/NavController/#isActive http://ionicframework.com/docs/v2/api/navigation/NavController/#isActive

Read the doc above.阅读上面的文档。 Import NavController to your Page, then you can inject NavController into your Page using typescript:NavController导入您的页面,然后您可以使用NavControllerNavController注入您的页面:

constructor(public nav: NavController) {}

or using ES6:或使用 ES6:

constructor(nav: NavController) {
    this.nav = nav;
}

After all that, you can call this.nav.getActive() to see the current Page or call this.nav.isActive('MyPage') to check if you are on MyPage毕竟,您可以调用this.nav.getActive()来查看当前页面或调用this.nav.isActive('MyPage')来检查您是否在 MyPage

Edit: the ES6 version isn't truly ES6 but should work for Ionic2编辑:ES6 版本不是真正的 ES6,但应该适用于 Ionic2

Old question but here are my 2 cents.老问题,但这是我的 2 美分。

I believe a more cleaner solution would be to make use of the id property in the NavOptions, a draw back is that you need to pay attention to the id's you assign to avoid an id collision:我相信更简洁的解决方案是使用 NavOptions 中的 id 属性,缺点是您需要注意分配的 id 以避免 id 冲突:

You instantiate the component like this:您像这样实例化组件:

this.navCtrl.push(MyComponent, navParams, { id: 'my-component-1' });

Then you can simply check it like this:然后你可以像这样简单地检查它:

this.navCtrl.getActive(true).id === 'my-component-1'

If you want to check for different Pages component this.navCtrl.last().instance instanceof MyPage;如果要检查不同的 Pages 组件 this.navCtrl.last().instance instanceof MyPage; should be sufficient if you want to compare between two pages instance of the same component the only option i found was use a ref to the container and next use the getBoundingClientRect function because the width of not visible pages is 0 this maybe can help to someone.如果您想在同一组件的两个页面实例之间进行比较,应该就足够了,我发现的唯一选项是使用对容器的引用,然后使用 getBoundingClientRect 函数,因为不可见页面的宽度为 0,这可能对某人有所帮助。

this.content.nativeElement.getBoundingClientRect().width > 0 this.content.nativeElement.getBoundingClientRect().width > 0

I track it, a little less elegant but effective and simpler:我跟踪它,不太优雅但有效且简单:

ionViewWillEnter(){
    this.isActualView=true;
}

ionViewWillLeave(){
    this.isActualView=false;
}

...

if(this.isActualView)...//here I know I am in the actual view or not

Updated answer for 2021 -- If you are using Angular for your Ionic project, this is how you can achieve the answer to the question: 2021 年的更新答案——如果您在 Ionic 项目中使用 Angular,那么您可以通过以下方式获得问题的答案:

<div routerLink="/tabs/tab1" [routerLinkActive]="'is-active'" #discover>
  <img [src]="discover.classList[0] == 'is-active' ? 'assets/tabs/explore-orange.png' : 'assets/tabs/explore-white.png'" />
  <ion-label>Home</ion-label>
</div>

[routerLinkActive]="'is-active'" will add the is-active class to that div when that route is active. [routerLinkActive]="'is-active'"将在该路由处于活动状态时将 is-active 类添加到该 div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM