Home > Mobile >  Can't resolve all parameters for AuthService on Angular
Can't resolve all parameters for AuthService on Angular

Time:06-01

I'm trying to start my angular app and i'm getting this error below for some reason. I tried removing the auth service provider from my component and removing also the auth service from my constractor inside my component, but nothing changed... I can't figure out what i'm doing wrong and i'm beginner on angular.

The error:

Can't resolve all parameters for AuthService: (?, ?, [object Object]).

My component:

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../../Services/auth.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  providers: [AuthService]
})

export class LoginComponent {

  isUserLoggedIn: boolean;
  emailAddress: string;
  password: string;
  invalidLogin: boolean;
  invalidText: string;

  constructor(private authService: AuthService, private router: Router) {

    if (authService.getCurrentUser() != null) {
      router.navigate(['/home']);
    }
  
  }

  login() {
    
    if (!this.authService.login(this.emailAddress.toString(), this.password.toString())) {
      this.invalidLogin = true;
      this.invalidText = "Wrong Email Address or password";
    }
    
  }

}

My service:

import { Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { UserModel } from "../Models/UserModel";

export class AuthService {

    constructor(private router: Router, private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) {}

    login(email: string, password: string) {

        const headers = {'Content-Type': 'application/json', };
        const body = { emailAddress: email, password: password };
        let userLoggedIn: Boolean = false;

        this.http.post<any>(this.baseUrl   "Api/Login", body, { headers }).subscribe(response => {
            
            if (response.username != null) {
                let user: UserModel
                user = response;
                this.router.navigate(['/home']);
                this.saveUserToLocalStorage(user);
                userLoggedIn = true
            }

        }, error => console.error(error)); 
        return userLoggedIn;
    }

    saveUserToLocalStorage(loggedInUser: UserModel) {
        sessionStorage.setItem("loggedInUser", JSON.stringify(loggedInUser));
    }

    getCurrentUser() {
        return sessionStorage.getItem("loggedInUser")
    }

    logout() {
        sessionStorage.removeItem("loggedInUser")
        this.router.navigate([''])
    }

    createUser(userData: UserModel) {}

    sendResetPasswordEmail() {}

}

CodePudding user response:

I think this is because you use Router in your constructor, try to do this

constructor(private readonly injector: Injector,
           ...) {}

public get router(): Router {
   return this.injector.get(Router);
}

CodePudding user response:

I found what was the problem. I had to mark my service class as @Injectable()

Like this:

@Injectable()
export class SchoolService {
  ...
}
  • Related