如何从Angular库中导出模型?

时间:2018-12-26 13:38:32

标签: angular angular-library

我创建了一个Angular库,但是我想导出一个模型,供我的应用程序使用。我该怎么办?

例如:

我的图书馆

library-model.ts

export class LibraryModel{
  //some model data
}

my-library.component.ts

import { Component, OnInit, Input } from '@angular/core';

//some imports    

@Component( {
    selector: '...',
    templateUrl: '...',
    styleUrls: [...]
} )
export class MyLibraryComponent implements OnInit {

    @Input() libInputData: LibraryModel;

    // some other codes
}

my-library.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { MyLibraryComponent} from './my-library.component';
import { LibraryModel} from './library-model';

@NgModule( {
    declarations: [MyLibraryComponent, LibraryModel],
    imports: [
        BrowserModule
    ],
    exports: [MyLibraryComponent, LibraryModel]
} )
export class MyLibraryModule { }

public_api.ts

export * from './lib/my-library.service';
export * from './lib/my-library.component';
export * from './lib/my-library.module';
export * from './lib/library-model';

我的应用

app.component.ts

import { Component } from '@angular/core';
import { LibraryModel } from 'my-library';

@Component({
  selector: 'grc-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'my-application';

  libraryModel : LibraryModel ;

  initializeData() {

     //initialize and set data for libraryModel 

  }
}

app.component.html

<my-lib-component libInputData="libraryModel" ></my-lib-component>

但是,通过这种设置,在构建库期间出现“无法导出值LibraryModel ...”错误。 我想使用LibraryModel,以便可以轻松地在app.component.html中传递数据。 我该如何实现?

1 个答案:

答案 0 :(得分:0)

您不能声明/导出它,它是一个模型,简单的非角类不是component,请从两个数组中将其删除({{ 1}}和declarations)。它已由 exports 导出。

  

不要声明以下内容:

     
      
  • 已经在另一个NgModule中声明的类
  •   
  • 从另一个包导入的指令数组。例如,不要从@ angular / forms声明FORMS_DIRECTIVES
  •   
  • NgModule类
  •   
  • 服务类别
  •   
  • 非角度类和对象,例如字符串,数字,函数,实体模型,配置,业务逻辑和   助手类
  •   

MDN.

Don't declare (Official doc)

相关问题