Error: StaticInjectorError (AppModule) [HttpClient]:

0

I have the following code in TypeScript, the problem is placed when the constructor passed the variable validarUsuario gives me this error in the browser console:

  

Error: Error: StaticInjectorError (AppModule) [HttpClient]

What is the correct way to use that variable?

    import { Component, OnInit } from '@angular/core';
    import { Router } from '@angular/router';
    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import {NgForm} from '@angular/forms';
    import { AppRoutingModule, routingComponents } from 'src/app/app-routing.module';
    import { ElementRef, ViewChild} from '@angular/core';
    import { LoginService } from '../services/login.service';

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

    @NgModule({
      declarations: [
        routingComponents
      ],
      imports: [
        BrowserModule,
            AppRoutingModule
          ],
          providers: [],
        })
        export class LoginComponent implements OnInit {
          @ViewChild('ipId') span: ElementRef; 

          constructor(private router: Router /*,private validarUsuario: LoginService*/) { }
          public laRespuesta = [];
          ngOnInit() {
          }
          login(form: NgForm) {
            if (form.value.txtEmail === '' && form.value.txtPassword === '') {
              alert('Ingrese su usuario y contraseña');
              return;
            } else if (form.value.txtEmail === '') {
              alert('Ingrese su usuario');
              return;
            } else if ( form.value.txtPassword === '') {
              alert('Ingrese su contraseña');
              return;
            } else {
              let esint = false;
              const UrlSitio = window.location.href.toString();
              if (UrlSitio.search('localhost:4200')) {
                esint = true;
              } else {
                esint = false;
              }
            }
          }
        }
    
asked by r0b0f0sn0w 18.07.2018 в 21:31
source

1 answer

1

The error tells you that you are not in the HTTP module, which you are surely using in LoginService .

You have to import it into your module, the best thing in your app.module.ts file:

import { HttpClientModule} from "@angular/common/http";
// ..
  imports: [
    BrowserModule,
    HttpClientModule,
    // ...
  ]
 //...

This way, in your LoginService there will no longer be a problem that you do not find HttpClient

    
answered by 19.07.2018 / 06:36
source