typescript ionic 3预先调用所有构造函数

标签 typescript ionic-framework ionic2

如果没有在所有页面构造函数中登录而不重复自己,我如何调用重定向?

  constructor(protected appCtrl: AppController) {
    this.appCtrl.redirectIfNotLogedIn();
  }

最佳答案

我建议有一个 BaseComponent 类来实现 ionViewCanEnter返回一个 bool 值。

Runs before the view can enter. This can be used as a sort of "guard" in authenticated views where you need to check permissions before the view can enter

export class BaseComponent{
  constructor(){}

  ionViewCanEnter(){
    //check if logged in or not and return the boolean value
  }

}

这个组件可以被你所有的页面扩展。

  export class MyPage extends BaseComponent{
  //...
  }

可以尝试在 BaseComponent 的构造函数中调用您的 this.appCtrl.redirectIfNotLogedIn();,但我建议您使用 NavGuard。

关于typescript ionic 3预先调用所有构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43930015/

相关文章:

Typescript React 组件装饰器

angular - typescript 对象类型转换不起作用

typescript - 返回的表达式类型 Child 不能分配给类型 Parent

angularjs - Ionic 未反射(reflect)父范围的更改

android - 从谷歌商店下载后应用程序崩溃了一次

sorting - firebase 和 Ionic 2 上的降序 orderByChild()

android - Android 设备上的 Ionic 2 中未显示 Typescript sourcemaps

TypeScript:创建具有可写属性的子类

Windows 10 移动版 - 从图库中选择时应用程序崩溃

javascript - 在单个结果页面上显示 JSON 索引时出错