简体   繁体   中英

How to use a library of an angular application within a library

I have an angular application with defined services. and I also have a couple of libraries defined. what I can't do is use an application service within one of my libraries.

  • src
    • app
      • service
        • myappservice.service.ts
  • project
    • mylib
      • src
        • lib
          • mylibservice.service.ts

when I try to import "myappservice" into "mylibservice" it gives me this error:

... is not under 'rootDir' .... 'rootDir' is expected to contain all source files.

mylibservice.service.ts

import { MyAppService} from 'src/app/services/myappservice.service';

@Injectable({
  providedIn: "root",
})

export class ObjectTypesService {
...
constructor(
    private myappservice: MyAppService
  ) {
...

Can someone help me with to use this service within my library?

You can build your lib module, publish it to npm and add it in package.json via npm.

You can build your lib module and instead of publishing it to npm you can import.tgz file in package.json.

You can use some monorepo that already solve this problem like https://nx.dev/ . In this solution you store all libs and app in same git repo and you can import libs to apps without publishing it to npm after every change. This has dowside because you must build every time app and all libs.

Is posible build lib in this monorepo as npm package too. If you need use it in other app that is outside monorepo or in app from monorepo as npm dependency.

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