I'm using version 17 of Angular and I would like to know how to use CanActivate, which has...

Discussão em 'Angular' iniciado por vinudev, Novembro 25, 2024.

  1. vinudev

    vinudev Guest

    I have the following code in my old version of Angular

    import { Injectable } from '@angular/core';
    import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from
    '@angular/router';
    import { Observable } from 'rxjs';
    import { AutorizacaoService } from '../services/autorizacao.service';


    @Injectable({
    providedIn: 'root'
    })

    export class AutorizadoGuard implements CanActivate {
    constructor(
    private autorizadoService: AutorizacaoService,
    private routerService: Router
    ){}

    canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> |
    boolean | UrlTree {
    const usuarioEstaLogado = this.autorizadoService.obterLoginStatus();
    if (usuarioEstaLogado)
    return true;
    this.routerService.navigate(["/login"]);
    return false;
    }

    }


    how do I reuse the same code structure, but adapting it to the new version and also using CanActivateFn?

    Continue reading...

Compartilhe esta Página