I have 2 classes (in reality one base class and many others).
I would like to get the parent context in child class without having to populate back super()every time. The base aim of that it to split my angular component into multiple classes. I will try make an example
//export.ts
export * from './parent'
export * from './child'
//parent.ts
import {fabric} from 'fabric'
import {Child} from './child'
export class Parent {
//many other variables here
canvas:fabric.Canvas = undefined;
childTest:Child = undefined;
width:number = undefined;
height:number = undefined;
//many other variables here too
constructor(){
}
initCanvas(canvasId:string, options:fabric.ICanvasOptions){
this.canvas = new fabric.Canvas(canvasId, options);
//here canvas is populated
this.childTest = new Child('this is only a test')
}
}
//child.ts
import { Parent } from './export'
import {fabric} from 'fabric'
export class Child extends Parent {
constructor(test:string){
super();
let rect = new fabric.Rect([0,0, 10, 10], {
//lots of options here
})
this.canvas.add(rect) // returns canvas is undefined
}
}
// something.component.ts
import { Component, OnInit, AfterViewInit, Renderer2, ElementRef, Inject} from '@angular/core';
import { Parent } from '../class/export'
//some code
parent:Parent = undefined
ngAfterViewInit(){
this.parent = new Parent();
this.parent.initCanvas('myCanvas', {
//a lot of options
})
}
so as you can see I cannot retrieve this.canvas and work with it is there any workarounds. I know I could pass the canvas into the method but I would prefer just like in a component to use the thiskeyword to access a global context.
so basically what I would like to do is:
call ngAfterViewInit()
|_ call Parent()
|_ call Parent.initCanvas() from parent and instanciate the canvas variable
|_ call Child() and retrieve the upper canvas variable instantiated
Any help is appreciated.
canvas:fabric.Canvas = undefined;so offcourse it will be undefined until you don'tcallinitCanvasmethod in your parent classthis.parent.initCanvasfrom some something component and you wantthis.canvasto not beundefinedin your child class?