6

Without importing OnInit and implements

ngOnInit(){
  this.recipies=this.recipeService.getRecipies();
} 

This is still working fine without any complain so why should i use like :

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

export class RecipiesListComponent implements OnInit{}
ranakrunal9
  • 13,320
  • 3
  • 42
  • 43
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85

2 Answers2

8

Interface are optional as per the angular documentation so you can use it without importing and implementing it.

Angular instead inspects directive and component classes and calls the hook methods if they are defined but it's good practice to add interfaces to TypeScript directive classes in order to benefit from strong typing and editor tooling.

ranakrunal9
  • 13,320
  • 3
  • 42
  • 43
1

We have to import OnInit in order to use like this (actually implementing OnInit is not mandatory but considered good practice).

import {Component, OnInit} from 'angular2/core';

Check this Link. Hope you got your answer.

Community
  • 1
  • 1
Avnesh Shakya
  • 3,828
  • 2
  • 23
  • 31