1

I have two services which one of them(Logger) is going to be used by another(SettingService), I have already searched a lot and see other questions and answers to this problem but no luck, I've already read this document angualr.io/When the service needs a service

here is the code for two services

Logger as a service

@Injectable()
export class Logger {

    public logs: string[] = [];
    //do stuffs
}

SettingService

@Injectable()
export class SettingService
{   
    private url = '/setting';
    private headers = new Headers({ 'content-type': 'application/json' });

    constructor(private http: Http,private logger: Logger) { } //problem is here
    //do stuffs   
}

the settingService is using both http and logger, I have a component app.component which uses settingService, here is my app.component and app.module

app.component.ts

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css']
})
export class AppComponent implements OnInit {

    constructor(private settingService: SettingService) { }
}

app.module.ts

    @NgModule({
  imports:      [ BrowserModule, FormsModule, HttpModule ],
  declarations: [AppComponent],
  providers:    [Logger, SettingService],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

but unfortunately I get the error which says : (SystemJS) Can't resolve all parameters for SettingService: ([object Object], ?).Error: Can't resolve all parameters for SettingService: ([object Object], ?) what is wrong??

this is plunker, that's not working!

7
  • @ParidOkht this may sound silly but can you try providing it in appcomponent instead of appmodule once . I know you want services to be singleton but just try once Commented Aug 20, 2017 at 8:49
  • also if possible can you create a small plunker to demonstarte the issues as the code listed here is fine , there must be something else Commented Aug 20, 2017 at 8:52
  • @RahulSingh I tried providing it in app.component, no luck Commented Aug 20, 2017 at 9:26
  • actually it works for me but i use webpack cli Commented Aug 20, 2017 at 9:40
  • @Vega how should I find that? Commented Aug 20, 2017 at 12:01

3 Answers 3

1

Try to change the order of import services into AppModule or try to use forwardRef() in settingService, such as constructor(private http: Http, @Inject(forwardRef(() => Logger)) private logger: Logger)

Sign up to request clarification or add additional context in comments.

1 Comment

yes you were right, but I saw your answer late,the problem was about import ordering of my service in app.module.ts, I explained it in one answer. thanks
0

One thing you can try is providing the logger in the bootstrap providers like this

bootstrap(AppComponent, [ 
    provide(Logger, {useClass: Logger},
    ....) 
]);

Comments

0

the error was ERORR: Can't resolve all parameters for Custom Service: ([object Object], ?)

I am using a index.ts file under services folder to export my services and then I import that index file in app.module.ts, the order of exporting in index.ts which comes to importing in app.module is important. it should be from least dependency to most dependency as my experience please be careful about these bellow thing:

  1. Avoid Circular Dependency
  2. Be Careful about register ordering of services in app.module provider
  3. Be careful about the import ordering of services in app.module.ts, which are dependent to each other, It must be ordered form low dependency to high dependency, otherwise you will get error injecting a service to another service. so, If you are using index.ts and export all your services there and then import index.ts in your app.module.ts you must be careful about the export ordering of your classes in index.ts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.