简体   繁体   English

在 Ionic 5 路由器中登录/注销时登录/主页不会被破坏

[英]Login/Home pages not destroyed when login/logout in Ionic 5 router

My project is in IONIC 5 and Firstore.我的项目在 IONIC 5 和 Firstore 中。 There are 2 different ion-router-outlet for authenticated (Home) and unauthenticated (Index) routes.有 2 种不同ion-router-outlet用于经过身份验证的 (Home) 和未经身份验证的 (Index) 路由。 Following is the code for dynamically opening the login/home page for users in class app.component.ts .以下是在 class app.component.ts中为用户动态打开登录/主页的代码。

  this.afAuth.authState.subscribe(user => {
    if (user) {
      this.router.navigateByUrl('home');
    } else {
      this.router.navigateByUrl('index');
    }
  }); 

Flow: Login Page -> (login) -> Home Page -> (logout) -> Login Page.流程:登录页面->(登录)->主页->(注销)->登录页面。 When the Home page is open the Login page is still loaded and in the navigation stack.当主页打开时,登录页面仍然加载并在导航堆栈中。 The ngOnDestroy of the Login page does not execute.登录页面的ngOnDestroy不执行。 After the logout, the Login page opens again but the class constructor and ngOnInit method does not execute.注销后,登录页面再次打开,但 class constructorngOnInit方法不执行。 This is causing a lot of issues as the page initialization is not reloading.这会导致很多问题,因为页面初始化没有重新加载。 The same issue happening for flow Home Page -> (logout) -> Login Page -> (login) -> Home Page.流程主页->(注销)->登录页面->(登录)->主页发生了同样的问题。 How can I destroy the Login page after login and Home page after logout so that those can reload if reopened in the same session?如何在登录后销毁登录页面和注销后的主页,以便在同一个 session 中重新打开时可以重新加载?

EDIT:编辑:

Following is the routing config:以下是路由配置:

app-routing.module.ts应用程序路由.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)
  },
];

Both the Home.html and Index.html has the same following code. Home.htmlIndex.html具有相同的以下代码。

<ion-content>
  <ion-router-outlet></ion-router-outlet>
</ion-content>

index-routing.module.ts索引路由.module.ts

const routes: Routes = [
  {
    path: '',
    component: IndexPage,
    children:[
      {
        path: '',
        loadChildren: () => import('../pages/login/login.module').then( m => m.LoginPageModule)
      },
    ]
  }
];

home-routing.module.ts家庭路由.module.ts

const routes: Routes = [
  {
    path: '',
    component: HomePage,
    children:[
      {
        path: '',
        loadChildren: () => import('../pages/user/user.module').then( m => m.UserPageModule)
      },
.....
Other authenticated pages
]

Easiest solution that would work, you should navigate to the route with "replaceUrl" NavigationExtras最简单的解决方案,您应该使用“replaceUrl”NavigationExtras 导航到路线

this.router.navigate(['/home'], { replaceUrl: true }); this.router.navigate(['/home'], { replaceUrl: true });

This replaces the current state in the history, and therefore your Login page will be destroyed.这将替换历史记录中当前的 state,因此您的登录页面将被销毁。 Basically it sets a new root.基本上它设置了一个新的根。

Reference参考

The seemingly unexpected behavior you are describing is due to Ionic.您描述的看似意外的行为是由于 Ionic 造成的。 More specifically, it is due to how Ionic deals with the life of a page .更具体地说,这是由于Ionic 如何处理页面的生命周期

When you navigate to a new page, Ionic will keep the old page in the existing DOM, but hide it from your view and transition the new page.当您导航到新页面时,Ionic 会将旧页面保留在现有 DOM 中,但将其隐藏在您的视图中并转换新页面。

... ...

Because of this special handling, the ngOnInit and ngOnDestroy methods might not fire when you would usually think they should.由于这种特殊处理,ngOnInit 和 ngOnDestroy 方法可能不会在您通常认为应该触发的时候触发。

ngOnInit will only fire each time the page is freshly created, but not when navigated back to the page. ngOnInit 只会在每次新创建页面时触发,但不会在导航回页面时触发。 For instance, navigating between each page in a tabs interface will only call each page's ngOnInit method once, but not on subsequent visits.例如,在选项卡界面中的每个页面之间导航只会调用每个页面的 ngOnInit 方法一次,而不会在后续访问时调用。 ngOnDestroy will only fire when a page "popped". ngOnDestroy 只会在页面“弹出”时触发。

Without knowing much about your application, I would suggest using the Ionic Lifecycle events instead of the Angular ones for your page components.在不了解您的应用程序的情况下,我建议您对页面组件使用Ionic Lifecycle 事件而不是 Angular 事件。 It sounds like you can probably just replace ngOnInit with ionViewWillEnter and replace ngOnDestroy with ionViewWillLeave or ionViewDidLeave .听起来您可能只需将ngOnInit替换为ionViewWillEnter并将ngOnDestroy替换为ionViewWillLeaveionViewDidLeave

Further down in the documentation is some useful guidance for each lifecycle method文档的下方是每个生命周期方法的一些有用的指导

I think you are facing the problem for destroying the previous page.我认为您正面临破坏前一页的问题。 In Iconic when you navigate to the other page, it still maintains the previous one in the DOM.在 Iconic 中,当您导航到另一个页面时,它仍然保留 DOM 中的前一个页面。

So your ngOnDestroy() is not being called.所以你的 ngOnDestroy() 没有被调用。

You can just replace your navigate() function to the navigateRoot() .您可以将您的navigate() function 替换为navigateRoot() This would resolve your issue and would completely gets destroyed.这将解决您的问题,并会完全被破坏。

So your line of codes should be somewhat look like:所以你的代码行应该有点像:

 this.afAuth.authState.subscribe(user => {
  if (user) {
    this.router.navigateRoot('home');
  } else {
    this.router.navigateRoot('index');
  }
 }); 

I hope this would resolve your issue.我希望这能解决您的问题。

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

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