1

I want a life cycle hook to be called when ever my component is active on the main page.

its login page.

when ever my component is on the main page i.e is active i want to see if i already have login token from previous login and depending on the result i want to navigate to different page.

is there any `ngOnActive' hook if not then how to create it?

or is there is any other way to achieve this?

my code:

import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import {LoginService} from './login.service';
import {TokenService} from '../data/token.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  providers: [LoginService]
})
export class LoginComponent implements OnInit {

  constructor(private loginService: LoginService, private tokenService: TokenService, private router: Router) { }

  ngOnInit() {
    if (this.tokenService.getToken() !== '') {
      console.log(this.tokenService.tk);
      this.router.navigate(['/admin']);
    }
  }
  onSubmit(form: NgForm) {
    this.loginService.login(form.value.email, form.value.password);
  }
}
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Rahul
  • 975
  • 9
  • 25
  • You can create a function i.e: activate(), then call it in the component (controller) scope – Bilal Djaghout Aug 05 '18 at 14:13
  • it should be called every time when the component is visible – Rahul Aug 05 '18 at 17:40
  • Can you not do your checks when the component is created using the ngOnInit hook? Do you need to do some periodic checks or react to some focus event? – hagner Aug 05 '18 at 17:44
  • it is login page, when the login page is visible i want to check if i already have login token, if i have then i will navigate to main page or else user have to login. that why i need some hooks like that. – Rahul Aug 05 '18 at 17:51
  • ngOnInit is not solution for this. or is there other way i can do that – Rahul Aug 05 '18 at 17:52
  • But could you not check if the token exist when creating the login screen? Is there a need to check again later? Do you want to check if the user has logged on in another tab or something? – hagner Aug 05 '18 at 17:59
  • no, in angular login page is created only one time and `ngOnInit` is hook for that, but it can be displayed many time. suppose user accidentally clicks login page after login in, i should be able to check if token already exist i will navigate to different page. – Rahul Aug 05 '18 at 18:04
  • How are you showing or hiding the login page now? Would it not be easier to recreate the login page when needed instead of showing or hiding it? – hagner Aug 05 '18 at 18:18
  • it angular is recreating the component, then why constructor is running only once, it should run every time angular recreates it. angular is not doing it. i found one way i can achieve by `rout guards` in angular – Rahul Aug 05 '18 at 18:25

2 Answers2

0

You can use one trick to do this, and that is detect a route change.

    import { ActivatedRoute } from '@angular/router';
    
    constructor(
        private routerActive:ActivatedRoute
      ) {}
    
    ngOnInit() {
        this.routerActive.paramMap.subscribe(paramMap => {
            if (this.router.url === "/yourURL") {

              // ***** This runs whenever your page is displayed ******

            }
        })
}
-1

Here is a piece of code that will serve as an example:

(function () {
  angular.module('app', [])
    .controller('MainController', function() {
      activate();
      function activate() {
        console.log('Success'); // this log will appear when ever your controller loads
      }
    });
})();
Bilal Djaghout
  • 166
  • 3
  • 14