nikk wong
nikk wong

Reputation: 8690

Angular2 Impelements vs imports

I'm going through the Angular2 guide, and though the keyword implements is used in class definitions it is not really described thoroughly in the docs.

For example, in the following:

export class CrisisDetailComponent implements OnInit, CanDeactivate {

  crisis: Crisis;
  editName: string;

  cancel() {
    this.editName = this.crisis.name;
    this.gotoCrises();
  }

  save() {
    this.crisis.name = this.editName;
    this.gotoCrises();
  }
}

OnInit is brought into the class by the implements keyword, but I've most commonly seen OnInit imported via import {Component, OnInit} from 'angular2/core' which is then used in class definitions.

What is the utility of the implements keyword? And, OnInit is not used directly in this class definition, so why is it being brought into the class at all? Is implements making OnInit available to the goToCrises() method? If so, why couldn't you just inject OnInit into the component that defined goToCrises()?

Thanks for any help.

Upvotes: 2

Views: 4588

Answers (1)

basarat
basarat

Reputation: 276255

OnInit is brought into the class by the implements keyword

But you still need to have the import to bring OnInit into the file.

PS:

implements only brings in the type information and not any runtime. You can easily remove OnInit and no functionality would change, its just making sure that the class follows the structure of OnInit

Upvotes: 6

Related Questions