Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-constructor-vars breaks DI #106

Closed
m5rk opened this issue Jun 22, 2016 · 5 comments
Closed

no-constructor-vars breaks DI #106

m5rk opened this issue Jun 22, 2016 · 5 comments

Comments

@m5rk
Copy link
Contributor

m5rk commented Jun 22, 2016

I copied the tslint.json from this repository and updated my code to conform to the rules. That seemed to break my app. If I declare a provider in the ionicBootraps function:

ionicBootstrap(MyApp, [Authentication]);

but then declare its visibility like so:

export class MyClass {
  private auth: Authentication;

  constructor(auth: Authentication) {
  }

MyClass's auth is undefined. Whereas if I do this instead:

export class MyClass {
  constructor(private auth: Authentication) {
  }

It Just Works™.

@lathonez
Copy link
Owner

You need to set the class's instance of auth to the one passed in the constructor:

export class MyClass {
  private auth: Authentication;

  constructor(auth: Authentication) {
    this.auth = auth;
  }

Please reopen if this doesn't work.

@m5rk
Copy link
Contributor Author

m5rk commented Jun 22, 2016

That works. Thank you for the quick reply.

I'm left with this question: Which is better--not manually assigning constructor parameters or not declaring parameters in the constructor?

@m5rk
Copy link
Contributor Author

m5rk commented Jun 22, 2016

Based on the way Ionic 2 generates classes, I think they've answered that question a certain way.

@lathonez
Copy link
Owner

IMO explicit is better than implicit.

When you implicitly declare in the constructor it isn't clear that those variables are being assigned to the class (this), hence your confusion when your removed the private and found that it was no longer assigned.

It's all very subjective, but I think clearer is better.

@lathonez
Copy link
Owner

#191

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants