简体   繁体   中英

Order of exports in barrel file for provided services in Angular 2 changes DI behavior

This took me a while to figure out. I'd greatly appreciate an explanation.

I keep all my core service files in a common folder which I then add to a barrel file called common.ts.

I import these services in app.module and provide them.

It appears the ordering of the services as exported from the barrel file makes a difference in the DI behavior.

When I run with the exports in this order

export { Core } from './core';
export { TestService } from './test-service';

I receive

Can't resolve all parameters for OtherService: (?)

OtherService is a dummy service that is not included in the barren file as it is component specific for my testing. OtherService is provided in app.module and is listed after TestService (not sure if that matters, but this has been a very weird day).

If I change the order of the exports in the common.ts barrel file to

export { TestService } from './test-service';
export { Core } from './core';

OtherService will receive the TestService just fine. The only other interaction I can think of is that CoreService is injected into app.component, but it has no dependency on TestService.

Why would changing the order of exports in a barrel file modify the behavior of the Angular DI engine? That's crazy talk if you ask me.

If necessary I can try to reproduce online, but I'll wait for a request to do so.

Thanks for any input, Mike

TL;DR It seems there is no official answer.

Angular 2 - Dependency Injection and Barreling

This post has some insight but most importantly, it mentions that while it is indeed true that export order matters, it is not officially documented as such.

I would think of this in the same way I'd think of ordering my imports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM