Non-angular problem5 with HttpClient

0

I'm trying to make a request with angular5 for learning purposes and I'm not with you, follow the console log and the source code.

ERROR Error: Uncaught (in promise): Error: StaticInjectorError[InvestimentosService]: 
  StaticInjectorError[InvestimentosService]: 
    NullInjectorError: No provider for InvestimentosService!
Error: StaticInjectorError[InvestimentosService]: 
  StaticInjectorError[InvestimentosService]: 
    NullInjectorError: No provider for InvestimentosService!
    at _NullInjector.get (core.js:923)
    at resolveToken (core.js:1211)
    at tryResolveToken (core.js:1153)
    at StaticInjector.get (core.js:1024)
    at resolveToken (core.js:1211)
    at tryResolveToken (core.js:1153)
    at StaticInjector.get (core.js:1024)
    at resolveNgModuleDep (core.js:10585)
    at NgModuleRef_.get (core.js:11806)
    at resolveNgModuleDep (core.js:10585)
    at _NullInjector.get (core.js:923)
    at resolveToken (core.js:1211)
    at tryResolveToken (core.js:1153)
    at StaticInjector.get (core.js:1024)
    at resolveToken (core.js:1211)
    at tryResolveToken (core.js:1153)
    at StaticInjector.get (core.js:1024)
    at resolveNgModuleDep (core.js:10585)
    at NgModuleRef_.get (core.js:11806)
    at resolveNgModuleDep (core.js:10585)
    at resolvePromise (zone.js:824)
    at resolvePromise (zone.js:795)
    at eval (zone.js:873)
    at ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (core.js:4620)
    at ZoneDelegate.invokeTask (zone.js:424)
    at Zone.runTask (zone.js:192)
    at drainMicroTaskQueue (zone.js:602)
    at ZoneTask.invokeTask [as invoke] (zone.js:503)
    at invokeTask (zone.js:1540)

Follow the codes

inversiones.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms'
import { InvestimentosService } from './investimentos.services'
import { Investimentos } from './investimentos.model'
// import 'rxjs/add/observable/from'
// import { Observable } from "rxjs/Observable";


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

  Form: FormGroup
  namberPattern = /^[+-]?([0-9]*[.])?[0-9]+$/
  porcentagemDoSeuSELICAno: number
  porcentagemDoSeuSELICMes: number
  // valordoJurosSelic:number
  arSelics: any[] = []
  arCdis: any[] = []
  arPoupancas: any[] = []
  taxas: Investimentos

  constructor(private fb: FormBuilder, private investimentosService: InvestimentosService) { }


  ngOnInit() {
    this.investimentosService.taxas()
    .subscribe(taxas => this.taxas = taxas)
  }
}

investments.model.ts

export interface Investimentos {
    selic: string
    cdi: string
}

inversiones.services.ts

import { Injectable } from '@angular/core'
import { HttpClient,HttpParams } from '@angular/common/http'
import { Observable } from 'rxjs/Observable'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'
import { Investimentos } from "./investimentos.model"


@Injectable()
export class InvestimentosService {
    constructor(private http: HttpClient) { }

    taxas(): Observable<Investimentos> {

        return this.http.get<Investimentos>('http://localhost:8080/aeua/cora/particular/all/backend/index.php')
    }
}
    
asked by anonymous 06.12.2017 / 21:51

1 answer

0

You need to register the module InvestimentosService as a provider in the module.

Explaining:

All classes that use the @Injectable() decorator must be registered in the list of angular providers so that it can do the dependency injection. To do this, go to the file *.module.ts (the same file that is importing your component) and include your service in the part of providers, eg:

@NgModule({
  declarations: [
     InvestimentosComponent
  ],

  providers: [
   InvestimentosService // <-- referência do seu service
  ]
})
    
06.12.2017 / 22:04