Angular HttpClient causing component disappearance

Viewed 32

I am trying to set up a login system using angular. To do so, I use the HttpClient. I have set up a user service and a login form using this service as a provider. If the user service uses HttpClient as a provider, components using the user service as a provider don't appear, otherwise they do. Using a router, I am automatically redirected to my previous location, and defining one as main component (see "app.component.html") causes nothing to appear.

I used dependency injection to pass providers, and I have defined "HttpClientModule" and "UserService" as providers in my "app.module.ts" file. There is no console error. Everything seems okay according to every tutorial I saw online, if you could help me figure out what's wrong I would really appreciate it.

Here are all the files related to this problem :

  providers: [
    HttpClientModule,
    CookieService,
    UserService
  ],

app.module.ts

import { Component, OnInit } from '@angular/core';
import { UserService } from 'src/app/services/user.service';
import { CookieService } from 'ngx-cookie-service';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  constructor(private srvLogin: UserService, 
      private router: Router, 
      public activatedRoute: ActivatedRoute, 
      private cookieService: CookieService) {  
  }

  ngOnInit() {}
}

login.component.ts

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { User } from '../class/User';

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private http : HttpClient) {}  
  //constructor(){} displays the components perfectly
  insertUser(user : User){
    this.http.post<User>('http://localhost:3000/users', user);
  }
}

user.service.ts

<app-login></app-login>

app.component.html

0 Answers
Related