简体   繁体   English

在Angular中重用组件

[英]Reusing components in Angular

I have 3 types of objects: 我有3种类型的对象:

A
 - FirstName
 - LastName
B
 - FirstName
 - LastName
 - MiddleName
C
 - FirstName

I need to create a component for creating these objects with a simple form that allows me to fill in the fields and send a request. 我需要使用一个简单的表单创建一个用于创建这些对象的组件,该表单允许我填写字段并发送请求。

For each type, I created a component: 对于每种类型,我创建了一个组件:

a.component.ts: a.component.ts:

@Component({
  selector: 'add-a-type',
  template: `
    <form [formGroup]="aForm">
  <div class="form-group">
    <label for="firstName">First Name</label>
    <input type="text" id="firstName" class="form-control" formControlName="firstName" />
  </div>

  <div class="form-group">
    <label for="lastName">Last Name</label>
    <input type="text" id="lastName" class="form-control" formControlName="lastName" />
  </div>

  <button type="button" class="btn btn-info" (click)="addA()" [disabled]="aForm.invalid">
    Add
  </button>
</form>
  `
})
export class AddAComponent implements OnInit {
  aForm: FormGroup;

  constructor(
  ) { }

  ngOnInit() {
    this.createAForm();
  }

  createAForm(): void {
    this.aForm = new FormGroup({
      firstName: new FormControl('', [Validators.required]),
      lastName: new FormControl(''),
    });
  }

  addA(): void {
    // code
  }
}

For the remaining components, everything will be the same, only the reactive form and the template itself will differ (there will be more or less fields). 对于其余组件,一切都将相同,只有反应形式和模板本身会有所不同(会有更多或更少的字段)。 Tell me how to avoid duplication in this case? 告诉我在这种情况下如何避免重复?

Create a component FormComponent like this: 创建一个这样的组件FormComponent:

@Component({
  selector: 'app-form',
  template: `
    <form [formGroup]="form" (submit)="onSubmit()">
      <div class="form-group" *ngIf="firstName">
        <label for="firstName">First Name</label>
        <input
          type="text"
          id="firstName"
          class="form-control"
          formControlName="firstName"
        />
      </div>

      <div class="form-group" *ngIf="middleName">
        <label for="middleName">Middle Name</label>
        <input
          type="text"
          id="middleName"
          class="form-control"
          formControlName="middleName"
        />
      </div>

      <div class="form-group" *ngIf="lastName">
        <label for="lastName">Last Name</label>
        <input
          type="text"
          id="lastName"
          class="form-control"
          formControlName="lastName"
        />
      </div>

      <button
        type="button"
        class="btn btn-info"
        (click)="submitBtn()"
        [disabled]="form.invalid"
      >
        Add
      </button>
    </form>
  `
})
export class FormComponent implements OnInit {
  @Input() firstName: boolean;
  @Input() middleName: boolean;
  @Input() lastName: boolean;
  @Output() formSubmit = new EventEmitter<any>();

  public form: FormGroup;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.form = this.fb.group({
      firstName: this.firstName ? new FormControl('', [Validators.required]) : undefined,
      middleName: this.middleName ? new FormControl('', [Validators.required]) : undefined,
      lastName: this.lastName ? new FormControl('', [Validators.required]) : undefined
    });
  }

  onSubmit() {
    if(this.form.valid) {
      this.formSubmit.emit(this.form.value);
    }
  }
}

And then use it on your pages. 然后在您的页面上使用它。 For example if you only want first & last name: 例如,如果您只想要名字和姓氏:

<app-form (formSubmit)="onSubmit($event)" [firstName]="true" [lastName]="true"></app-form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM