Paweł Ostromecki
Paweł Ostromecki

Reputation: 49

Angular service providedIn: root imported from library throws NullInjectorError

I moved service into seperated npm package. Although the service is marked to be provided in root injector when I try to use it in my app I get NullInjectorError. I need to include QueryParamServiceFactory within providers array what solves the problem but I'd like to use my service without adding in into providers array. What's wrong with my implementation?

@Injectable({
  providedIn: 'root',
})
export class QueryParamServiceFactory {
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private location: Location
  ) {}

  create<T>(
    filtersModel: T,
    defaultPagination: IPagination
  ): QueryParamService<T> {
    return new QueryParamService<T>(
      this.router,
      this.route,
      this.location,
      filtersModel,
      defaultPagination
    );
  }
}

Upvotes: 0

Views: 2064

Answers (1)

J. S.
J. S.

Reputation: 2376

Do you mean by moving the service into a seperate npm package that you have to install it and add it as a dependency into your package.json?

Because if so, than provided in root won't work. Angular analyzes all injectables for this annotation and add it to the top of the injection tree. But it won't analyze the node_modules folder, that is why your service is not available in the injection tree automatically

Upvotes: 2

Related Questions