Typescript Inheritance With Angular 2
Inheritance should just be create a abstract base component class, then create the implementations and add the needed metadata.
Inheritance in Typescript
abstract class Person { constructor() {} protected Born() {} // born the same for all instances abstract GetName(); // Name is unique to the instance } class Person extends Ance { constructor(name) { super(); // must call the parent first thing // `this.name` is set be TypeScript magic } public GetName = () => this.name; }
Can I use `extends` and `implements` together?
Yep, `extends` can be used with `implements but only if it is first.
Order is important.
Make sure the abstract class is before the usage.
This is easily enforce by putting the `abstract into it's own file and importing it. but above the `@Component` will also work.
It's a bit of a shame the ts compiler can't fix this for us.
Getting injected dependancies (ie servies) into the base class
You're note actually creating the base class, so why would angular inject into it.
Just pass them through the child constructor. A la..
//child constructor(protected _myService: MyService) { super(_myService); }
Angular Life-Cycle Hooks.
The lifecycles hooks work as expected. Just set them on the base class.