这是个新手的问题,所以请容忍我。我想抽象ng2-table,将它封装在一个可重用的组件中。首先,我定义了一个模板:
table.comp.html
<ng-table [config]="config"
[rows]="rows" [columns]="columns">
</ng-table>然后,我定义了组件:
table.comp.ts
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
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,我在以下模块中导入它:
@NgModule({
declarations: [
CallingComponent
],
imports: [
BrowserModule,
TableModule
],
bootstrap: [CallingComponent]
})
export class CallingModule {}以及相关部分:
@Component({
selector: 'some-selector',
template: '<my-table></my-table>'
})
export class CallingComponent {}我在浏览器控制台中得到以下错误:
“‘my table”不是已知的元素: ..。
如何解决这个问题?
发布于 2018-07-19 16:38:36
您需要导出组件以便在其他模块中可用。
例如:
@NgModule({
declarations: [
TableComponent
],
exports: [
TableComponent // <-- this was missing
],
imports: [
BrowserModule,
Ng2TableModule
],
bootstrap: [TableComponent]
})
export class TableModule {}https://stackoverflow.com/questions/51392619
复制相似问题