I have been working with Angular 2 since first alpha, but I got stucked at creating traversable tree from components.
I have separate data source of component configurations and settings, which is persisted and also reacts to some events of the associated component. This source is tree which is represented by angular components in View. Tree nodes matches exactly the tree of angular components.
Components inherits from same abstract class BaseComponent. Component can contain any number of child instances of BaseComponent so it doesn't know exact type of parent/child.
I'd like to avoid specifying some kind of key through component inputs.
Is there any way how to get parent using base type?
TL;DR: I need to build parent/child tree of following components without knowledge of exact type
export abstract class BaseComponent {
public children: Array<BaseComponent>;
constructor(
//I need something like this but it's not working for ancestor classes
@Inject(forwardRef(() => BaseComponent))
private _parent: BaseComponent)
{
_parent.children.push(this);
}
}
@Component({...})
export class A extends BaseComponent {...}
@Component({...})
export class B extends BaseComponent {...}
@Component({...})
export class C extends BaseComponent {...}
@Component({...})
export class D extends BaseComponent {...}