如果没有在所有页面构造函数中登录而不重复自己,我如何调用重定向?
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/