Encountering the following error: "ERROR Error: Uncaught (in promise): Error: NG0200: Circular dependency in DI detected for UserService."
The auth.component.ts utilizes the UserService and User classes, while the user.service.ts only uses the User class.
The origin of the circular dependency is unknown.
User.model.ts:
export class User{
constructor(public login: string,
public Mdp : string){
}
}
User.Service.ts :
import 'rxjs/RX';
import { User } from '../models/User.model';
import { Subject } from 'rxjs';
import { Injectable } from '@angular/core';
@Injectable()
export class UserService {
private users!: User[];
userSubject = new Subject<User[]>();
constructor(private userService: UserService) {
}
emitUsers() {
this.userSubject.next(this.users.slice());
}
addUser(user: User) {
this.users.push(user);
this.emitUsers();
}
logUser(){
console.log("la");
}
}
auth.component.ts:
import { ThisReceiver } from '@angular/compiler';
import 'rxjs/RX';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormArray } from '@angular/forms';
import { Router } from '@angular/router';
import { User } from '../models/User.model';
import { Subscription } from 'rxjs/Subscription';
import { UserService } from '../services/user.Service';
@Component({
selector: 'app-auth',
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.scss']
})
export class AuthComponent implements OnInit, OnDestroy {
users: User[] = [];
userSubscription: Subscription = new Subscription;
userForm!: FormGroup;
constructor(
private UserService: UserService,
private formBuilder: FormBuilder,
private router : Router
) {}
ngOnInit() {
this.initForm();
}
initForm(){
this.userForm = this.formBuilder.group({
login:['',Validators.required],
Mdp:['',Validators.required],
});
}
onSubmitForm(){
const formValue= this.userForm.value;
const newUser = new User (
formValue['login'],
formValue['Mdp']
);
console.log(newUser);
this.UserService.logUser();
}
ngOnDestroy() {
this.userSubscription.unsubscribe();
}
}
Grateful for any guidance on resolving this issue.