[英]How to import component into another component in Angular
I created a component called CopySchedulefromSiteComponent and I want to import it into another component called SiteScheduleComponent.我创建了一个名为 CopySchedulefromSiteComponent 的组件,我想将它导入另一个名为 SiteScheduleComponent 的组件。 I am not sure how to do this correctly
我不确定如何正确执行此操作
CopySchedulefromSiteComponent has one field in it using Formly CopySchedulefromSiteComponent 使用 Formly 在其中有一个字段
CopySchedulefromSiteComponent CopySchedulefromSiteComponent
@Component({
selector: 'app-copy-schedule-from-site',
templateUrl: './copy-schedule-from-site.component.html',
styleUrls: ['./copy-schedule-from-site.component.scss'],
})
export class CopyScheduleFromSiteComponent implements OnInit {
showOverwriteWarningModal(): Observable<boolean> {
const deleteRef = this.dialog.open(CopyScheduleModalComponent, {});
return deleteRef.afterClosed();
}
copySiteScheduleControl: FormlyFieldConfig | undefined;
modelLoaded = false;
form = new FormGroup({});
options: FormlyFormOptions = {};
fields: FormlyFieldConfig[] = [
{
fieldGroupClassName: 'row',
fieldGroup: [
{
wrappers: ['form-field'],
className: 'col-6',
key: 'siteScheduleControl',
type: 'select',
templateOptions: {
label: 'Copy Schedule from Site',
options: this.approvedSites,
labelProp: 'text',
},
expressionProperties: {
'templateOptions.disabled': (): boolean => {
return this.siteSubmissionModel.disableControls;
},
},
hooks: {
onInit: (field: FormlyFieldConfig | undefined): void => {
this.copySiteScheduleControl = field;
if (field?.templateOptions?.options) {
field.templateOptions.options = this.approvedSites;
}
},
},
},
],
},
];
I want to have the formly field from CopySchedulefromSite component connected/imported to SiteScheduleComponent formly fields.我希望将 CopySchedulefromSite 组件中的正式字段连接/导入到 SiteScheduleComponent 正式字段。 I imported the component but get ''CopyScheduleFromSiteComponent' is declared but its value is never read'.
我导入了组件,但得到“声明了 CopyScheduleFromSiteComponent”,但它的值从未被读取。 Included some formly fields below
包括下面的一些正式字段
SiteScheduleComponent站点调度组件
import {CopyScheduleFromSiteComponent} from './copy-schedule-from-
site/copy-schedule-from-site.component';
@Component({
selector: 'app-site-schedule',
templateUrl: './site-schedule.component.html',
styleUrls: ['./site-schedule.component.scss'],
})
export class SiteScheduleComponent implements OnInit, OnChanges {
@Input() siteSubmissionModel: SiteSubmissionModel = new SiteSubmissionModel();
copySiteScheduleControl: FormlyFieldConfig | undefined;
model: SiteScheduleModel = {
siteScheduleControl: undefined,
monthsOfOperation: new CoreApi.MonthsOfOperation(),
daysOfOperation: {days: []},
hoursOfOperation: {days: []},
exception: [],
};
modelLoaded = false;
form = new FormGroup({});
options: FormlyFormOptions = {};
fields: FormlyFieldConfig[] = [
{
fieldGroupClassName: 'row',
fieldGroup: [
{
wrappers: ['form-field'],
className: 'col-6 mt-small',
type: 'button',
templateOptions: {
text: 'Copy',
matIcon: 'file_copy',
onClick: (): void => {
if (this.model.monthsOfOperation && (this.model.monthsOfOperation.fromDate ||
this.model.monthsOfOperation.toDate)) {
this.showOverwriteWarningModal().subscribe((confirmed) => {
if (confirmed) {
this.copySchedule();
}
});
} else {
this.copySchedule();
}
},
},
expressionProperties: {
'templateOptions.disabled': (): boolean => {
return !this.model.siteScheduleControl || this.siteSubmissionModel.disableControls;
},
},
},
],
},
{
template: '<h4>Months of Operation</h4>',
},
{
fieldGroupClassName: 'row',
fieldGroup: [
{
className,
fieldGroup: [
{
key: 'monthsOfOperation',
type: FORMLY_CUSTOM_TYPE_DATE_RANGE_PICKER,
templateOptions: {
fromDate: 'monthsOfOperation.fromDate',
toDate: 'monthsOfOperation.toDate',
required: true,
onDateChange: (): void => this.onMonthsOfOperationChanged(),
},
expressionProperties: {
'templateOptions.disabled': (): boolean => {
return this.siteSubmissionModel.disableControls;
},
},
},
],
},
Please advise a solution for this.请为此提供解决方案。
What you actually want to do is you want to use one Component inside of another.您真正想要做的是您想在另一个组件中使用一个组件。 You almost never import a Component into another.
您几乎从不将组件导入另一个组件。
To use Component A inside Component B you need to have them either declared inside the same Module or Component A to be imported into Module where Component B is being declared.要在 Component B 中使用 Component A,您需要将它们声明在同一个 Module 中,或者将 Component A 导入到声明 Component B 的 Module 中。
If that requirement is met you can use Component A inside Component B by adding Component A's selector into the html of Component B. Then it is possible to communicate between the Components in a few ways, for example using their Inputs and Outputs.如果满足该要求,您可以通过将组件 A 的选择器添加到组件 B 的 html 中来在组件 B 中使用组件 A。然后可以通过几种方式在组件之间进行通信,例如使用它们的输入和输出。
To read more on Components and Modules:要阅读有关组件和模块的更多信息:
https://angular.io/api/core/Component https://angular.io/api/core/Component
https://angular.io/guide/architecture-modules https://angular.io/guide/architecture-modules
To grasp the basics of using Components:要掌握使用组件的基础知识:
https://angular.io/tutorial/toh-pt3 https://angular.io/tutorial/toh-pt3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.