我的项目使用IONIC 5和Firestore。有两个不同的
ion-router-outlet
,分别用于验证(Home)和非验证(Index)路由。以下是在类app.component.ts
中为用户动态打开登录/主页的代码。 this.afAuth.authState.subscribe(user => {
if (user) {
this.router.navigateByUrl('home');
} else {
this.router.navigateByUrl('index');
}
});
流程:登录页面 -> (登录) -> 主页 -> (注销) -> 登录页面。当主页打开时,登录页面仍在加载并处于导航栈中。登录页面的 ngOnDestroy
不会执行。注销后,登录页面再次打开,但类的 constructor
和 ngOnInit
方法不会执行。这将导致很多问题,因为页面初始化不会重新加载。同样的问题也出现在“主页 -> (注销) -> 登录页面 -> (登录) -> 主页”的流程中。
如何在登录后销毁登录页面,在注销后销毁主页,以便在同一会话中重新打开时可以重新加载?
编辑:
以下是路由配置:
app-routing.module.ts
const routes: Routes = [
{
path: 'home',
canLoad: [AuthGuard],
loadChildren: () => import('./home/home.module').then(m => m.HomePageModule)
},
{
path: 'index',
loadChildren: () => import('./index/index.module').then(m => m.IndexPageModule)
},
];
Home.html
和Index.html
都有相同的以下代码。
<ion-content>
<ion-router-outlet></ion-router-outlet>
</ion-content>
index-routing.module.ts
const routes: Routes = [
{
path: '',
component: IndexPage,
children:[
{
path: '',
loadChildren: () => import('../pages/login/login.module').then( m => m.LoginPageModule)
},
]
}
];
home-routing.module.ts
const routes: Routes = [
{
path: '',
component: HomePage,
children:[
{
path: '',
loadChildren: () => import('../pages/user/user.module').then( m => m.UserPageModule)
},
.....
Other authenticated pages
]
ionViewWillEnter()
、ionViewDidEnter()
、ionViewWillLeave()
和ionViewDidLeave()
,而不是使用ngOnDestroy
和ngOnInit
? - julianobrasil