0

I been trying to migrate my Ionic 1 application to Ionic 2 and have been encountering alot of new terms and problems.

I do not really understand the need for get parameters() and when and what variables to return in that function.

For example(To Navigate):

 static get parameters() {
    return [[NavController]];
 }

Previously in an older ionic build(I'm not sure which version I was developing upon), I can navigate to pages without this function. In the latest ionic 2 build(2.0.0-beta.25) , without this get parameters() function, it just wont navigate to the next targeted page , but there are no errors.

I'm aware of a existing post: Angular/Ionic 2 - what is a provider and what does `static get parameters()` do?

Therefore, in what occasion I should return and what variables do I return?

Community
  • 1
  • 1
Gene
  • 2,178
  • 3
  • 30
  • 50

2 Answers2

1

In ES6 dependency injection gets the list of types it needs to resolve and pass instances for to the constructor of the class.

It has to be static, otherwise they could not be read before an instance is created.

In TypeScript it can aquire these types from the constructor parameters if they are applied (or @Inject() annotations).

Return the types for the constructor parameters of your class in the order they are listed in the constructor parameter list.

See https://stackoverflow.com/a/34546344/217408 for an example.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

In fact, Angular2 tries to find out what to inject into the constructor of the class using the parameters method of the ReflectionCapabilities class:

This method tries several things:

  • within the parameters of the type (i.e. a class property, i.e. static property)

    @Component({
      (...)
    })
    export class SomeComponent {
      constructor(http) {
      }
    
      static get parameter() {
        return [[Http]];
      }
    }
    
  • within the parameters metadata for the class if any. It's populated by @Inject decorator. Not that parameter decorators aren't supported in ES6

    @Component({
      (...)
    })
    export class SomeComponent {
      constructor(@Inject(Http) http) { // == http:Http
      }
    }
    
  • within the design:paramtypes metadata that is internally created using the parameter types of the constructor. With ES6, this metadata isn't available since types for method parameters can't be used.

    @Component({
      (...)
    })
    export class SomeComponent {
      constructor(http:Http) {
      }
    }
    

Based on these types, Angular2 looks for corresponding providers to get instances to inject.

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360