Rachel
Rachel

Reputation: 113

How to consume a service from Angular Library in a project

I have a Angular 6 project which import an angular library.

I import components from library successfully, and now I'd like to have the AuthGuard Service in library, to share in all projects, but I can't do it.

I exported the AuthGuard in lib's public_api and provided in module, but I don't know how to access in project.

When I try to import like:

import { AuthGuard} from 'my-lib/lib/security/auth.guard';

I have the error:

Module not found: Error: Can't resolve 'my-lib/lib/security/auth.guard' in 'c:\workspace\my-project\src\app\portal'

Upvotes: 7

Views: 7208

Answers (2)

faisaljanjua
faisaljanjua

Reputation: 936

Error seems you are missing some part.

Check
1.In app.module.ts

import { AuthGuard } from 'my-lib';  

2. add AuthGuard in providers providers: [AuthGuard, ...]

3.In Module import { AuthGuard } from 'my-lib';

Upvotes: 5

Tomasz Kula
Tomasz Kula

Reputation: 16837

You should import it directly from the lib.

import { AuthGuard } from 'my-lib'

If this is not possible than you have incorrectly configured your lib.

Upvotes: 0

Related Questions