import service from library

21 views
Skip to first unread message

Alexander Mills

unread,
Dec 22, 2020, 7:34:18 AM12/22/20
to Angular and AngularJS discussion
I created a library using:

$ ng g library tools

I want to import a service called AuthService from the library from another project, I have:

import {AuthService} from "../../../../tools/src/lib/auth.service";

is there a better way to resolve the library or is that about right?

Sander Elias

unread,
Dec 22, 2020, 7:39:09 AM12/22/20
to Angular and AngularJS discussion
Try

```ts
import {authService} from "tools"
```

Regards
Sander

Op dinsdag 22 december 2020 om 13:34:18 UTC+1 schreef alexande...@gmail.com:

Alexander Mills

unread,
Dec 22, 2020, 7:37:44 PM12/22/20
to Angular and AngularJS discussion
sadly that didn't work, I wonder if there is a way to specify it safely? I wouldn't want "tools" to collide with a package from node_modules?
Screen Shot 2020-12-22 at 4.36.52 PM.png

Sander Elias

unread,
Dec 22, 2020, 11:50:38 PM12/22/20
to Angular and AngularJS discussion
When you create a library, the CLI puts a path in your tsconfig.json.  You can look this up. the name you can use for your import should be in there. (if you did export it properly, it might be that you need to do a so-called "deep import" (`tools/something/else/my-thing`))
And no, I'm not aware of a way to guard against package name collisions. Just make sure you don't conflict.
For myself, I usually use a prefix. (for my Angular things its usually '@se-ng'

Op woensdag 23 december 2020 om 01:37:44 UTC+1 schreef alexande...@gmail.com:
Reply all
Reply to author
Forward
0 new messages