Daniel
Daniel

Reputation: 213

Angular 6 injected service can't be resolved

I have built an Angular 6 library 'auth' with the two services './src/lib/auth.service.ts' (AuthService) and './src/lib/error.service.ts' (ErrorService).

Now i want to use both services in my Angular 6 app - both injected into another service like:

constructor(
    private http: HttpClient,
    private authService: AuthService,
    private errorService: ErrorService
  ) {}

But when i try to compile the app the following error occures:

Module not found: Error: Can't resolve 'auth/lib/error.service' in 'C:\Projects\MyProject\src\app\services\data'

I can import the ErrorService without any problems and VSCode doesn't show any errors. Only whe i try to compile it.

The Library and the app are built with the same versions of Angular 6. I also added error.service.ts to public_api.ts and i provide it in auth.module.ts.

The AuthModule is imported into app.module.ts

...
import { AuthModule } from './auth';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    AuthModule
  ],
  providers: [
    ...
  ],
  entryComponents: [EditDialogComponent],
  bootstrap: [AppComponent]
})
export class AppModule {
  ...
}

Here is the file public_api.ts:

/*
 * Public API Surface of auth
 */

export * from './lib/auth.service';
export * from './lib/error.service';
export * from './lib/auth.component';
export * from './lib/auth.module';

And the file error.service.ts (still empty):

import { Injectable } from '@angular/core';

    @Injectable()
    export class ErrorService {

    }

I don't have any problems with the AuthService. What am i doing wrong?

Upvotes: 0

Views: 3153

Answers (1)

Asanka
Asanka

Reputation: 1648

AuthService and ErrorService should initiate before your third service. so please provide them in app module(root), then they will be singleton.

as this link

Upvotes: 1

Related Questions