[英]How can I use angular component imported from a shared module in another component other than AppComponent
Using Angular8使用 Angular8
I have created a module( ChartsModule
) that exports other components.我创建了一个导出其他组件的模块( ChartsModule
)。 I could import the module in app.module.ts
file and use the templates that are exported in app.component.html
, however trying the same by creating another component with a module that imports ChartsModule
does not work.我可以在app.module.ts
文件中导入模块并使用在app.component.html
中导出的模板,但是通过使用导入ChartsModule
的模块创建另一个组件来尝试相同的方法不起作用。
ChartsModule图表模块
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { PieChartComponent } from './pie-chart/pie-chart.component';
import { LinesChartComponent } from './lines-chart/lines-chart.component';
import { TableChartComponent } from './table-chart/table-chart.component';
@NgModule({
declarations: [PieChartComponent,LinesChartComponent,TableChartComponent],
imports: [
CommonModule
],
exports:[PieChartComponent,LinesChartComponent,TableChartComponent]
})
export class ChartsModule { }
Inside the app.module.ts在app.module.ts里面
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
NgbModule,
XComponent,
ChartsModule]
Using the templates inside app.component.html;使用app.component.html 中的模板;
<app-pie-chart></app-pie-chart>
<app-table-chart></app-table-chart>
QUESTION;题;
How can I achieve the same effect by importing ChartsModule
into component X. This is what I did;如何通过将ChartsModule
导入到组件 X 中来达到同样的效果。我是这样做的; generate component X and add a module that will import ChartsModule
and then to use the templates inside x.html
.生成组件 X 并添加一个模块,该模块将导入ChartsModule
,然后使用x.html
的模板。
x.module.ts x.module.ts
import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import { ChartsModule } from '../data-charts/charts.module';
@NgModule({
declarations: [],
imports: [
CommonModule,
ChartsModule
],
// schemas:[CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
})
export class XModule { }
Inside of x.component.html x.component.html 内部
<app-pie-chart></app-pie-chart>
<app-table-chart></app-table-chart>
I get the error;我得到了错误;
Uncaught Error: Template parse errors:
'app-pie-chart' is not a known element:
1. If 'app-pie-chart' is an Angular component, then verify that it is part of this module.
2. If 'app-pie-chart' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<router-outlet></router-outlet>
The component X
must be a member of some module.组件X
必须是某个模块的成员。 Import ChartsModule
there. ChartsModule
那里导入ChartsModule
。
The mistake is in your AppModule imports.错误在于您的 AppModule 导入。 You can't import components , only modules that has components.您不能导入组件,只能导入具有组件的模块。
In this case you must put your xComponent inside the declarations: []
of your xModule for a correct solution.在这种情况下,你必须把你的xComponent里面declarations: []
你xModule的一个正确的解决方案。
Anyways, if you want to use components that whitin ChartsModule you should declare your xComponent inside declarations: []
of any module that has ChartsModule inside his imports: []
无论如何,如果您想使用带有 ChartsModule 的组件,您应该在declarations: []
声明您的 xComponent declarations: []
的任何在其imports: []
中具有 ChartsModule 的模块imports: []
Hope it helps!希望能帮助到你!
You have to declare XComponent
in XModule
for the required result.您必须在XModule
声明XComponent
以获得所需的结果。
x.module.ts x.module.ts
import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import { ChartsModule } from '../data-charts/charts.module';
// Import `XComponent` here.
@NgModule({
declarations: [XComponent], // Just added reference.
imports: [
CommonModule,
ChartsModule
],
// schemas:[CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
})
export class XModule { }
This is how got it solved;这就是它解决的方法;
I imported XComponent into Xmodule, removed XComponent importation from app.module.ts and imported Xmodule inside app.module.ts;我将 XComponent 导入到 Xmodule 中,从 app.module.ts 中删除了 XComponent 导入,并在 app.module.ts 中导入了 Xmodule;
xmodule.ts xmodule.ts
import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import { ChartsModule } from '../data-charts/charts.module';
import {XComponent} from './x.component';
@NgModule({
declarations: [XComponent],
imports: [
CommonModule,
ChartsModule
],
})
export class XModule { }
app.module.ts app.module.ts
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
NgbModule,
Xmodule,
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.