1
resposta

[Dúvida] Erro de NullInjectorError

Bom dia, tudo bem ? Eu estou usando o Angular 14 e seguindo o tutorial do professor continua dando erro de NullInjectorError

Estou usando outra API

Service

import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";


const API = 'https://rickandmortyapi.com/api'


export interface Response{
    result:Array<PersonagemItem>
}

export interface PersonagemItem{
    name:string,
    image:string,
}

Injectable({ providedIn: 'root' })
export class PhotoService{

    constructor (private http:HttpClient){

    }

    listfromCharacter(character:string){
       return this.http
        .get<Response>(API+'/character')

    }
}

App component

import { Component } from '@angular/core';
import { PersonagemItem,Response, PhotoService } from './photos/photo/photo.service';





@Component({
  selector: 'app-root',
  templateUrl:  './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  character:PersonagemItem[]=[]

  constructor (photoService:PhotoService){
    photoService
    .listfromCharacter('all')
    .subscribe(characters=>this.character = characters.result)
  }

}


1 resposta

Olá Camille, normalmente esse erro esta relacionado com a falta da importação do modulo do HttpClient.

Você pode tentar adicionar ele no seu arquivo app.module.ts :

import { HttpClient, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }