0

Angular onDestroy detected issue

As you know destory event isn't detected at the following cases

  • enter key down at URL bar
  • key down F5 key

Way to check the following case actually Token is alive

So I tried to check existing token the after above action. And I caught AppComponet class instance from ActivatedRoute. Token is saved into LocalStorage fast of all, so available to get manage data using the token. And return current page but Angular created new the component. I should destory all components before return current page but I'm not sure the way to that. Let me know the way if you know.

  async canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Promise<boolean> {

      // token check
      let result = this.authFactiry.CheckToken();
      if (result) {

        // get manage data for user
        result = await this.authFactiry.GetData();
      }

      if (result) { 
        this.destoryComponents(); // <- I would like to know this detail
        return result;
      }

      const comp = this.route.Component; // this is AppComponent

      // if check error navigate to login page
      this.router.navigate(['login']);
      return result;
  }
Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
  • In this case, ActivatedRoute.routeConfig is null, you know – Jun Takeshita Jan 23 '20 at 03:23
  • when you refresh it will destroy all the components ? what do you mean you want to destroy them ? – fastAsTortoise Jan 23 '20 at 03:39
  • check this link https://stackoverflow.com/questions/56340306/angular-component-are-not-getting-destroyed It is not perfect to complete this angular issue. Angular core create new Component for current Page after returned true from auth guard function. – Jun Takeshita Jan 23 '20 at 03:57
  • can you share you code at github? – Mahesh Bhatnagar Jan 23 '20 at 04:09
  • You don't need debug this code if you are not sure the matter – Jun Takeshita Jan 23 '20 at 04:17
  • In this case, if returned false instead of true, main content of current page was gone. – Jun Takeshita Jan 23 '20 at 04:18
  • I confirmed created AppComponent as new after returned true. I understood Angular core renew services... – Jun Takeshita Jan 23 '20 at 07:11
  • what you have to destroy? Angular is a single page application so when you type an URL or refresh the page with F5 you're actually destroy the components and reload everything, when you use routingin angular you're not loading a new page even though you see a different URL – GJCode Jan 30 '20 at 21:20
  • In our developing, Dom is not destoyed at pushed F5 or Enter After that Angular makes new app modules, lost control previous such like this https://stackoverflow.com/questions/56340306/angular-component-are-not-getting-destroyed So it is better we use 2 domains, one is for login other one is for main – Jun Takeshita Feb 03 '20 at 07:31

0 Answers0