【问题标题】:getting parent context of a class in TypeScript在 TypeScript 中获取类的父上下文
【发布时间】:2019-12-30 06:33:29
【问题描述】:

我有 2 个类(实际上是一个基类和许多其他类)。 我想在子类中获取父上下文,而不必每次都返回super()。其基本目的是将我的角度组件分成多个类。我会尝试做一个例子

//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
    })
  }

如您所见,我无法检索 this.canvas 并使用它是否有任何解决方法。我知道我可以将画布传递给方法,但我更喜欢在组件中使用 thiskeyword 来访问全局上下文。

所以基本上我想做的是:

call ngAfterViewInit()
     |_ call Parent()
     |_ call Parent.initCanvas() from parent and instanciate the canvas variable
         |_ call Child() and retrieve the upper canvas variable instantiated

感谢任何帮助。

【问题讨论】:

  • 你设置了canvas:fabric.Canvas = undefined;,所以除非你不在父类中调用initCanvas方法,否则它将是未定义的
  • 对不起,我忘记添加组件了。感谢您的评论
  • @jitender 完成了更改
  • 所以您从某个组件调用this.parent.initCanvas,并且您希望this.canvas 在您的子类中不是undefined
  • @jitender 我想要一个全局上下文。我用所有主要变量初始化一个父类,然后访问子类中的一些变量。我不明白为什么在组件中实例化类应该是一个问题。非常感谢提前

标签: angular typescript inheritance


【解决方案1】:

添加一个保存画布的 Angular 服务。 你有两个选择 1.在服务上添加方法,如:

yourCanvasService.addStuffThatUsedToBeInChild1(..). 

yourCanvasService.addStuffThatUsedToBeInChild2(..).
  1. 保留旧的 Child 类,但不要让它们扩展 Parent。而是在类构造函数中注入 Angular 服务,然后使用该服务更常用的方法。

    export class ARectangleClass{
        constuctor(private theService:MyCanvasService){
            theService.addShape();
        }
    }
    

【讨论】:

  • 如果您在将实际画布保留在服务中时遇到任何问题,那么只需在服务中存储一个模型,以便画布可以从中进行绘制
【解决方案2】:
export class Parent {
    protected canvas:fabric.Canvas;
    constructor(){}

    protected 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')
    }
}

还有孩子

export class Child extends Parent {

    constructor(test:string){
      super();

    }

    init(canvasId,options){
      this.initCanvas(canvasId);
      let rect = new fabric.Rect([0,0, 10, 10], options)
      this.canvas.add(rect) 
    }

}

在组件中

ngAfterViewInit(){
    this.child= new Child();
    this.child.init('myCanvas', {
      //a lot of options
    })
  }

【讨论】:

  • 非常感谢您的回答。对不起,我真的不明白其中的逻辑。我已经完成了课程以避免在组件中设置所有内容。在您的示例中,我正在像以前一样做所有事情。必须有一种方法来创建全局上下文并将组件拆分为不同的文件并保留上下文
  • 好吧,抱歉,我刚刚更改了代码,所以它以标准方式使用了父类。随着我的改变,画布将被设置。这是你想要成为全球性的画布吗?
  • 好吧,对不起,我必须向自己解释得很糟糕。主要思想是例如。当我在组件上使用 this 关键字时,我可以在所有函数中访问它。现在我想将我的组件拆分为类并将我的基类的this 用于我的孩子。因此,如果我的this.canvas 在我的父类中实例化,我想将它用于我的子类,就像我将canvas 全局变量用于我的组件一样。非常感谢提前
  • 是的,在我的示例中,您需要在孩子 ngAfterViewInit 中调用 this.initCanvas。你想避免这种情况吗?如果那是问题,我想知道了
  • ngAfterViewInit() 不是它的上层的孩子。调用顺序是 (ngAfterViewInit(Parent(Child()))),其中使用实例化的画布在子级中检索父级的上下文
猜你喜欢
  • 2010-09-21
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
相关资源
最近更新 更多