V
V
Vadimqa2019-12-28 22:39:37
typescript
Vadimqa, 2019-12-28 22:39:37

How to pass data from one class to another in one module in angular or combine them?

Please do not laugh, I'm weak, I can't master the logic of the typescripts. I'll try to explain what doesn't work with comments in the script. Here's what I'm trying to do: combine the authorization check API call module and the router module.

import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router";
import { Observable } from "rxjs";
import { UserService } from './user.service';
import { Injectable } from '@angular/core';
import { NgModule } from '@angular/core';


@NgModule({
    providers: [ UserService, Test ]
  })


// Так я обращаюсь к апи в модулях компонентов
export class Test {

    user: any;

    constructor(private userService: UserService) { }

    ngOnInit() {
      this.userService.getUser().subscribe(data => {
          this.user = data;
          // Здесь происходит обращение к АПИ, который возвращает логин или false
          // Присваиваем переменной auth значение (конечно это не работает, здесь нужна помощь)
          if(this.user['login'] != false) {$auth = true;}
        });

    }
}

// Это текущий модуль, который должен отправить на форму входа со страниц, требующих авторизации
export class AuthGuard implements CanActivate {

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean> | boolean{

        // Здесь надо получить переменную auth из класса Test
        if($auth) {return true;} else {location('/login');}
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question