File

apps/live-weather/src/app/core/router-guards/validate-data-for-place-navigation.guard.ts

Index

Methods

Constructor

constructor(router: Router)

Creates instance of CityDetailsContainerComponent class

Parameters :
Name Type Optional Description
router Router No

instance of angular router

Methods

canActivate
canActivate(route: ActivatedRouteSnapshot)
Parameters :
Name Type Optional
route ActivatedRouteSnapshot No
Returns : Observable | Promise | boolean | UrlTree
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
/**
 * Validates the path before navigating to Place details
 */
export class ValidateDataForPlaceNavigationGuard implements CanActivate {
  /**
   * Creates instance of ```CityDetailsContainerComponent``` class
   *
   * @param router instance of angular router
   */
  constructor(private router: Router) {}

  /**
   * @internal
   */
  canActivate(
    route: ActivatedRouteSnapshot
  ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    if (!route.params.name || !route.params.id) {
      this.router.navigate(['/home']);

      return false;
    }

    return true;
  }
}

result-matching ""

    No results matching ""