Angular 如何将服务从一个角度库注入到另一个角度库

Angular 如何将服务从一个角度库注入到另一个角度库,angular,Angular,我必须在“项目”文件夹中创建库 ng generate library foo ng generate library bar My foo库包含一个公共_api.ts,用于包装库文件 export * from './lib/foo.service'; 我在foo中有一个服务定义为抽象类 export abstract class FooService {} 我想在酒吧服务中扩展餐饮服务 import { FooService } from 'projects/foo/src/publi

我必须在“项目”文件夹中创建库

ng generate library foo
ng generate library bar
My foo库包含一个公共_api.ts,用于包装库文件

export * from './lib/foo.service';
我在foo中有一个服务定义为抽象类

export abstract class FooService {}
我想在酒吧服务中扩展餐饮服务

import { FooService } from 'projects/foo/src/public_api';
export class BarService extends FooService{}
我犯了这个错误

BUILD ERROR
error TS6059: File '/projects/foo/src/lib/foo.service.ts' is not under 'rootDir' 'projects/bar/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '/projects/foo/src/public_api.ts' is not under 'rootDir' '//projects/bar/src'. 'rootDir' is expected to contain all source files.

如何从bar中获取foo库?

尝试将库视为一个单独的项目(即使它们位于单个源代码树中)

如何使你的方法有效:(从头开始)

  • ng新libs测试
  • ng生成库foo
  • ng生成库栏
  • ng build foo
  • npm-ifoo@file:dist/foo
    //这会将应用程序项目的本地依赖项添加到foo库中
  • 打开库
    栏的package.json,添加
    “foo”:“latest”
    依赖项:
  • 请注意非相对的
    从'foo'导入{FooService}

  • 构建您的
    bar
    库:
    ng构建bar
  • 就这样!您的
    bar
    库正确使用了
    foo
    功能

    如果您想在应用程序中使用
    的服务,只需执行以下操作:

  • ng构建栏
  • npm-ibar@file:dist/bar
  • AppComponent
    中:
  • 从'@angular/core'导入{Component}; 从'bar'导入{BarService}; @组成部分({ 选择器:'应用程序根', templateUrl:“./app.component.html”, 样式URL:['./app.component.less'] }) 导出类AppComponent{ 标题=‘libs测试’; 建造商(私人酒吧服务:酒吧服务){ } }
  • 确保应用程序正确构建:
    ng build--aot
  • { "name": "bar", "version": "0.0.1", "peerDependencies": { "@angular/common": "^7.2.0", "@angular/core": "^7.2.0", "foo": "latest" // add this line } } import { Injectable } from '@angular/core'; import { FooService } from 'foo'; @Injectable({ providedIn: 'root' }) export class BarService extends FooService{ constructor() { super(); } } import { Component } from '@angular/core'; import { BarService } from 'bar'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.less'] }) export class AppComponent { title = 'libs-test'; constructor(private barService: BarService) { } }