首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将NG2-台封装在角度6中

将NG2-台封装在角度6中
EN

Stack Overflow用户
提问于 2018-07-18 02:16:21
回答 1查看 1K关注 0票数 0

这是个新手的问题,所以请容忍我。我想抽象ng2-table,将它封装在一个可重用的组件中。首先,我定义了一个模板:

table.comp.html

代码语言:javascript
复制
<ng-table [config]="config"
          [rows]="rows" [columns]="columns">
</ng-table>

然后,我定义了组件:

table.comp.ts

代码语言:javascript
复制
import { Component } from '@angular/core';

@Component({
  selector: 'my-table',
  templateUrl: 'table.comp.html',
  styles: []
})
export class TableComponent {
    
    columns:Array<any> = [
            {title: 'Name', name: 'name'},
            {title: 'Id', name: 'id'},
            {title: 'Age', name: 'age'},
          ];

    config:any = {
                paging: true,
                sorting: {columns: this.columns}
              };

 
    rows = [ .. // rows will be populated here  // .. ]
 }

最后,我定义了一个模块来导入NG2表:

table.module.ts

代码语言:javascript
复制
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { Ng2TableModule } from 'ng2-table/ng2-table';
import { TableComponent } from './table.comp';

@NgModule({
  declarations: [
      TableComponent
  ],
  imports: [
    BrowserModule,
    Ng2TableModule
  ],
  bootstrap: [TableComponent]
})
export class TableModule {}

为了调用my-table,我在以下模块中导入它:

代码语言:javascript
复制
@NgModule({
  declarations: [
    CallingComponent
  ],
  imports: [
    BrowserModule,
    TableModule
  ],
  bootstrap: [CallingComponent]
})
export class CallingModule {}

以及相关部分:

代码语言:javascript
复制
@Component({
      selector: 'some-selector',
      template: '<my-table></my-table>'
    })
export class CallingComponent {}

我在浏览器控制台中得到以下错误:

“‘my table”不是已知的元素: ..。

如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-19 16:38:36

您需要导出组件以便在其他模块中可用。

例如:

代码语言:javascript
复制
@NgModule({
  declarations: [
      TableComponent
  ],
  exports: [
     TableComponent   // <-- this was missing
  ],
  imports: [
    BrowserModule,
    Ng2TableModule
  ],
  bootstrap: [TableComponent]
})
export class TableModule {}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51392619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档