简体   繁体   English

覆盖子 ngOnInit 方法

[英]Override child ngOnInit method

I want to add code to child's ngOnInit method from a parent class.我想从父类向孩子的 ngOnInit 方法添加代码。 Can I somehow do something similar to this?我可以以某种方式做类似的事情吗?

export class Child extends Parent {
   constructor(){
      super(this.ngOnInit);
   }
   ngOnInit(){
      console.log('Child onInit');
   }
}

And Parent:和家长:

export class Parent {
   constructor(childOnInitMethod: any){}
   ngOnInit(){
      console.log('Parent added code to child onInit');
      this.childOnInitMethod.apply(this, arguments)
   }
}

The problem is that on the child constructor I do not have "this" available.问题是在子构造函数上我没有可用的“this”。

UPDATE: Find here the current code更新:在此处找到当前代码

For now, this is what I've done.目前,这就是我所做的。 Child class extends parent's ngOnInit and ngOnDestroy.子类扩展了父类的 ngOnInit 和 ngOnDestroy。 What I want to achieve is to avoid calling parent "super.subscribeEvents();"我想要实现的是避免调用父级“super.subscribeEvents();” like this:像这样:

ngOnInit() {
   window.onresize = () => console.log('resizing');
   super.subscribeEvents();
}

What I want to do is to add code to the Child's ngOnInit from Parent.我想要做的是从父级向子级的 ngOnInit 添加代码。 These are the classes:这些是类:

export class DatatablePage extends EventSubscriber {
   @Output() onLoadRow: EventEmitter<any> = new EventEmitter();
   public gridApi: GridApi;
   constructor(
    events: Events
   ) {
    super(events, [
        { eventName: 'datatable:updateList', callbackFunction: () => this.onLoadRow.emit() },
        { eventName: 'datatable:resizeTable', callbackFunction: () => this.gridApi.sizeColumnsToFit() }
    ])
   }

   ngOnInit() {
       window.onresize = () => console.log('resizing');
       super.subscribeEvents();
   }
}

And Parent:和家长:

export class EventSubscriber {


constructor(
    private events: Events,
    public eventSubscriptions: EventObject[]
) {
}

ngOnInit() {
    this.subscribeEvents();
}

ngOnDestroy() {
    this.unsubscribeEvents();
}

subscribeEvents() {
    this.eventSubscriptions.forEach((eventSubscription: EventObject) => {
        this.events.subscribe((eventSubscription.eventName), eventSubscription.callbackFunction)
    });
}

unsubscribeEvents() {
    this.eventSubscriptions.forEach((eventSubscription: EventObject) => {
        this.events.unsubscribe((eventSubscription.eventName), eventSubscription.callbackFunction)
    });
}
}

the below solution should work.以下解决方案应该有效。

class Parent {
   constructor(){}
   ngOnInit(){
      console.log('Parent added code to child onInit');
      this.ngOnInit()
   }
}

class Child extends Parent {
    ngOnInit() {
      console.log('Child onInit');
    }
    invokeSuperOnInit() { 
        super.ngOnInit();
    }
}

const a = new Child();
a.invokeSuperOnInit();

Execution:执行:

  1. Created object of Child Class and assigned to a.创建子类的对象并分配给a。

  2. calling invokeSuperOnInit Method on the object a.对对象调用 invokeSuperOnInit 方法 a.

  3. ngOnInit of the Parent is invoked.调用 Parent 的 ngOnInit。

  4. In the parent NgOninit We are calling ngOnInit of the Child.在父 NgOninit 中,我们正在调用 Child 的 ngOnInit。 The Child ngOnInit is invoked because "this" will always stay same through out the hierarchy.调用 Child ngOnInit 是因为“this”在整个层次结构中始终保持不变。

In order to call methods on your child you need to add it in your parent's .component in the following way:为了在您的孩子上调用方法,您需要通过以下方式将其添加到您父母的.component中:

@ViewChild(Child) child: Child;

After your parent and child are instantiated you can use this.child.anyFunction();在您的父母和孩子被实例化后,您可以使用this.child.anyFunction();

An alternative is to use @Input for your child like:另一种方法是为您的孩子使用@Input ,例如:

@Input() test: number;

The parent's view should look like this:父视图应如下所示:

<child [test]="example"></child>

While you define the variable in your component:在组件中定义变量时:

example: number;

ngOnInit() {
    this.example = 1;
}

Have also a look on ngOnChanges.也看看 ngOnChanges。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript用子方法覆盖父方法 - Javascript Override parent method with child method Angular ngOnInit 不适用于子组件 - Angular ngOnInit not working for the child component 在子类中密封一个方法,所以孙子不能覆盖它 - Making a method sealed in child class, so grandchildren cannot override it 尝试覆盖和扩展Typescript子类中的方法签名 - Trying to override and extend method signature in child class in Typescript 如何覆盖Javascript中的方法并将其应用于原始对象,而不是孩子? - How can I override a method in Javascript and apply it to the original object, not a child? Angular:如何刷新 ngOnInit 方法 - Angular: How to kinda refresh the ngOnInit method Angular 8 测试 ngOnInit 不调用服务方法 - Angular 8 test ngOnInit not calling service method 如何在Jasmine的单元测试中模拟super.ngOnInit()? 如何在基类/父类方法上创建间谍以仅覆盖派生/子类代码? - How to mock super.ngOnInit() in unit tests with Jasmine? How to create spy on Base / Parent class method to cover derived / child class code only? 子组件在初始化之前要等待父组件ngOnInit异步操作 - Child Component to wait for Parent Component ngOnInit async operation before initializing Angular 7 调用 ngOnInit 方法或组件包含的组件的任何其他方法 - Angular 7 call ngOnInit method or any other method of component that is contained by component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM