Hi I am trying to create a component that extends from another component, but code become very clumsy, as I must propagate constructor parameter to the child components. Is there a better way to structure the following code?
The base class is:
import { alerts } from './alerts';
import { BaseFirebaseDaoService } from './../providers/base-firebase-dao-service';
import { Injectable,Component,Inject } from '@angular/core';
import { NavParams } from 'ionic-angular';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { FirebaseListObservable } from 'angularfire2';
@Injectable()
@Component({
templateUrl: './dummy.html'
})
export class BaseFormHandler<T extends BaseFirebaseDaoService> {
form: FormGroup;
item;
dao:T;
constructor(public navParams: NavParams,
public fb: FormBuilder, public _alerts:alerts) {
this.item = this.navParams.get("item");
this.buildForm();
}
buildForm(){
//to be ovveride in sub clusses.
}
remove(){
this.dao.remove(this.item.$key)
.then((a:void) => {
this._alerts.presentToast("removed success");
})
.catch((a:Error) => {this._alerts.presentToast("remove fail")});
}
submit() {
let key = this.item.$key;
this.dao.update(key, this.form.value);
}
}
This is the extending class:
import { alerts } from './../../shared/alerts';
import { BaseFormHandler } from './../../shared/base-form-handler';
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MemberService } from '../../providers/member-service';
import { NavParams } from 'ionic-angular';
import { AngularFire } from 'angularfire2';
@Component({
templateUrl: 'members-form-page.html'
})
export class MembersFormPage extends BaseFormHandler<MemberService>{
constructor(public navParams: NavParams,
public fb: FormBuilder, af: AngularFire, public _alerts: alerts) {
super(navParams, fb, _alerts);
this.dao = new MemberService(af);
}
buildForm() {
this.form = this.fb.group({
name: [this.item.name, Validators.required],
email: [this.item.email, Validators.required]
});
}
}
So I must always send (and import) the constructor's parameters public navParams: NavParams, public fb: FormBuilder, public _alerts:alerts from all sub classes, although the sub-class not using them. This list can be longer…
Any alternative way for such implementation?
ReflectiveInjectorfrom@angular/core. The idea is to let the base component find the service that you want to use and return it.public,protected,private) in child class constructors, as those values will be assigned in parent class anyway.