0

I have service1 depending on an other service2.

Service2 has one parameter in its constructor.

When declaring service2 in the constructor of service1 like this

constructor(private service : service2) {...}

I have the following error : An argument for service1 is not provided.

The solution here Angular 2: Inject Service to another service. (No provider error) consisting to put service2 in app.module providers work for my components but not for the service1. I steel have the error : an argument for 'service2' is not provided.

for more details :

Service2 :

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';


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

  baseUrl = "http://127.0.0.1:8000";
  httpHeaders = new HttpHeaders({ 'Content-Type': 'application/json' })


  dataSource: BehaviorSubject<any> = new BehaviorSubject([]);
  list: Array<any> = [];

  constructor(private http: HttpClient) {
  }
...
}

service1 :

import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { ApiService } from './api.service';

@Injectable({
  providedIn: 'root'
})

export class FlowsheetEditorService {

  //declarations ...

  // httpHeaders = new HttpHeaders({ 'Content-Type': 'application/json' })
  
  constructor(private api : ApiService) { ...}
}

In app module :

providers: [ApiService],

bootstrap: [AppComponent]

Do you know why and can you help?

I don't know if that matter but I put all my services in a folder in the /app folder.

Thank you

Mahamane
  • 11
  • 1
  • 6

2 Answers2

0

You have to inject service into service then inject in customer and use another service function inside service

ANKIT MISHRA
  • 558
  • 4
  • 13
0

The problem was that I had initialized the service somewhere in my project without the parameter: which should not be done because angular initializes the services

Mahamane
  • 11
  • 1
  • 6