Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance in angular2

I'm trying to understand inheritance in angular 4.

Below is my AppComponent file:

import { Component,OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  title = 'app';
  myArray= ["1","2","3"]; //THIS WORKS
  //myArray:any; //Declared but not initialized

  setTitle(value:any){
    this.title=value;
    console.log(this.myArray[2]);

  }
  ngOnInit(){
    //this.myArray=["1","2","3"]; //THIS DOESNT WORK

  }
}

This is another component which extends AppComponent

import { Component, blurbs } from '@angular/core';
import { AppComponent } from '../app.component';

@Component({
  selector: 'app-child',
  templateUrl: `<p>
  title {{title}}
  <button (click)="setTitle('child')">change title</button>
</p>`,
  styleUrls: ['./child.component.css']
})

export class ChildComponent extends AppComponent implements OnInit {

  constructor() {
    super();
   }

  ngOnInit() {
  }

}

In the above code i'm calling the setTitle() method from the ChildComponent.

I have a console log for myArray[2].

The value works fine if i initialize the value while declaring it.

But if i just declare and initialize the array in the ngOnInit block the console.log doesnt work and tells me that myArray[2] is undefined.

I have a similar scenario in my application can anyone guide me what is the right approach and why its doesn't work when i initialize the array in ngOnInit block

like image 667
Shruti Nair Avatar asked Dec 21 '17 08:12

Shruti Nair


People also ask

Is there inheritance in angular?

Inheritance with Angular ComponentsWe can create a “parent” component with common properties/functions, followed by a child component that “extends” the parent. The child will inherit the parent's properties and functions but will have its own template, stylesheet and test file.

Are lifecycle hooks inherited?

Lifecycle methods are not inherited Lifecycle methods (OnInit, OnChanges, …) are not inherited by the child components.

What are decorators in angular2?

In Angular 2, decorators allow developers to configure classes as particular elements by setting metadata on them. The most commons are the @Component one, for components and the @Injectable one, for classes you want to inject dependencies in.

Where does the component metadata inherit from?

The component only inherits the class logic: All meta-data in the @Component decorator is not inherited.


1 Answers

ngOnInit method is redefined in ChildComponent, this line

   //this.myArray=["1","2","3"]; //THIS DOESNT WORK

is never executed.

If ChildComponent is supposed to inherit ngOnInit method from parent class, it shouldn't define its own method. If it's supposed to extend it, it should call parent's method:

  ngOnInit() {
    super.ngOnInit();
    ...
  }
like image 87
Estus Flask Avatar answered Sep 21 '22 14:09

Estus Flask