Angular 6 CLI工作区.如何创建导出服务的库 [英] Angular 6 CLI Workspaces. How to create library that exports services

查看:66
本文介绍了Angular 6 CLI工作区.如何创建导出服务的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

  • Angular CLI 6引入了工作区的概念.
  • 一个工作区可以包含多个项目.
  • 工作区和项目的配置位于工作区根文件夹中的"angular.json"文件中.
  • 每个项目可以是应用程序或库.
  • CLI可以使用以下命令生成一个项目,该项目是一个库:
         ng generate library forms-lib 

  • 此命令创建一个类型的项目 具有组件和服务的库",并导出该组件.
    • This command creates a project of type 'library' with a component and service and exports the component.
    • 问题:

      我试图创建一个库并使其中的服务可用于需要它们的应用程序中,但没有成功.

      I am trying, without success, to create a library and make the services in it available for use in applications which need them.

      此代码不起作用:

      import { NgModule } from '@angular/core';
      import { FormsLibComponent } from './forms-lib.component'; 
      import { FormsLibService } from './forms-lib.service';
      
      @NgModule({
          imports: [],
          declarations: [FormsLibComponent],
          exports: [FormsLibComponent, FormsLibService],
      })
      export class FormsLibModule {
      }
      

      返回错误:

      Uncaught Error: Can't export value FormsLibService from FormsLibModule
      as it was neither declared nor imported!
      

      有人可以指出我正确的方向吗?

      Can someone point me in the right direction?

      谢谢.

      推荐答案

      这可能为您解决问题:

      import { NgModule, ModuleWithProviders } from '@angular/core';
      import { FormsLibComponent } from './forms-lib.component'; 
      import { FormsLibService } from './forms-lib.service';
      
      @NgModule({
          declarations: [FormsLibComponent],
          exports: [FormsLibComponent],
      })
      export class FormsLibModule {
          static forRoot(): ModuleWithProviders {
              return {
                ngModule: FormsLibModule,
                providers: [FormsLibService]
              };
            }
      }
      

      这篇关于Angular 6 CLI工作区.如何创建导出服务的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆