当前位置: 首页 > 知识库问答 >
问题:

在 TypeScript 中获取类的父上下文

栾昂雄
2023-03-14

我有两个类(实际上是一个基类和许多其他类)。我希望获得子类中的父上下文,而不必每次都填充回< code>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
    })
  }

如您所见,我无法检索<code>这个。canvas并使用它,是否有任何解决方法。我知道我可以将画布传递到方法中,但我更希望像组件中一样使用<code>this<code>关键字来访问全局上下文。

所以基本上我想做的是:

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

任何帮助是值得赞赏的。

共有2个答案

连文栋
2023-03-14

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

yourCanvasService.addStuffThatUsedToBeInChild1(..). 

yourCanvasService.addStuffThatUsedToBeInChild2(..).

保留旧的子类,但不要让它们扩展父类。相反,在类构造函数中注入角度服务,然后使用服务更常见的方法。

export class ARectangleClass{
    constuctor(private theService:MyCanvasService){
        theService.addShape();
    }
}
葛驰
2023-03-14
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
    })
  }
 类似资料:
  • 问题内容: 我在构建ORM库时要考虑到重用和简单性。一切都进行得很好,除了我被愚蠢的继承限制所困。请考虑以下代码: 显然,这不是我所期望的行为(尽管实际行为也很有意义)。因此,我的问题是,你们是否知道在父类中获得子类名称的意思。 问题答案: 简而言之。这不可能。在php4中,您可以实施可怕的破解(检查),但是该方法在PHP5中不起作用。参考资料: 37684 34421 编辑 :PHP 5.3中后

  • 我正在尝试为属性创建get和set方法: 设置值的关键字是什么?

  • 我创建了一个类EventBus。这个类提供了一个接口,用于将on/off/once方法附加到一个类上,扩展它的类可以反过来提供一个可以被监听的可能事件的数组。 目前,为了使其工作,我需要使用EventBus扩展类。我觉得这不是正确的方法; EventBus使用方法而不是构造函数初始化,但也可以使用构造函数; 我想知道正确的方法是什么。我觉得使用EventBus扩展某些东西不是正确的继承,Event

  • 我想得到由接口实现的子类名。例如 在我的代码中,我声明了接口A,并将值设置为其中一个类。所以我有这样的: 现在我想得到当前的子类名称,比如B、C或其他什么,但我不想用instanceof,因为我有很多子类,这使得代码不必要太长。我基本上是在寻找这个,但是getChild()当然不存在。 提前谢谢!

  • 我试着用正常的方式做吐司。但我得到了错误,我尝试了很多倍数源,但失败了。

  • 本文向大家介绍JavaScript 获取元素在父节点中的下标(推荐),包括了JavaScript 获取元素在父节点中的下标(推荐)的使用技巧和注意事项,需要的朋友参考一下 jQuery中直接通过$(this).index()即可得到当前元素的下标。但原生JavaScript并没有提供类似的属性或方法,这时候可以调用数组中的indexOf方法直接计算 这里是计算elt在其父节点下,相同标签的元素中的