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

Dependency Injection failing when a second decorator is in class #4994

Closed
Jonatthu opened this issue Feb 24, 2017 · 16 comments
Closed

Dependency Injection failing when a second decorator is in class #4994

Jonatthu opened this issue Feb 24, 2017 · 16 comments
Labels
needs: investigation Requires some digging to determine if action is needed P5 The team acknowledges the request but does not plan to address it, it remains open for discussion type: bug/fix

Comments

@Jonatthu
Copy link

[x ] bug report => search github for a similar issue or PR before submitting

Current behavior
I have a class which I want to inject within it's constructor properties, they are classes too with @Injection() decorator,

@Injectable()
@remotedev({ name: 'STORE', onlyActions: true })
export class Store {
constructor(
    public alerts: AlertsStore,
    public client: ClientStore,
    public search: SearchStore,
    public searchListing: SearchListingStore
  ) {}

the other classes just have @Injectable() decorator and not a second decorator,
my index.ts to put the providers in the app module has the right order

export * from './alerts.store';
export * from './client.store';
export * from './search.store';
export * from './search-listing.store'
export * from './store';

and the providers looks like this

  providers: [
    AlertsStore,
    ClientStore,
    SearchStore,
    SearchListingStore,
    Store
  ],

and every property on the constructor is undefined. which is weird because on ionic 2 which is using @angular 2.2.1 is working normally without problems, but a project generated with ng cli is not working which is beta 32, and has angular ^2.4.0, so I'm not sure why this is working in ionic 2 and this not.

Expected behavior
Inject each class on the constructor

Minimal reproduction of the problem with instructions
https://github.com/Jonatthu/angular-dependency-injection-failing

What is the motivation / use case for changing the behavior?
Works fine on ionic 2 and this should work as well, without problems.

  • Angular version: 2.0.X
    Angular CLI beta.32.2, ionic has angular 2.2.1 and @angular from angular-cli is 2.4.2

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

  • Language: TypeScript

  • Node (for AoT issues): node --version = 6.9.2

@filipesilva
Copy link
Contributor

This sounds like a bug. Thanks for setting up the repro, we will investigate.

@filipesilva filipesilva added needs: investigation Requires some digging to determine if action is needed package4: @angular-sdk/webpack type: bug/fix P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful labels Mar 14, 2017
@filipesilva
Copy link
Contributor

#3583 might be related (and I think it should have fixed it at the time).

@Jonatthu
Copy link
Author

@filipesilva I forgot to mention that you need to install redux-remote dev chrome extension too

@sumitarora
Copy link
Contributor

@Jonatthu This seems to be an issue with the https://github.com/zalmoxisus/mobx-remotedev chrome extension. It works perfectly without it as soon as you enable extension it breaks the flow.

@filipesilva
Copy link
Contributor

Closing as answered.

@Jonatthu
Copy link
Author

@sumitarora
Copy link
Contributor

@Jonatthu It could be that as they are extending class and then assigning it back which might be affecting the constructor parameters. Still, it would require running the app as well as dev tools running simultaneously both in dev mode to check what is exactly causing this behavior.

@zalmoxisus
Copy link

Seems like the issue happens for any extended class. Here's a generic decorator to replicate the issue:

function extendClass(target: typeof Store): typeof Store {
  return class extends target {
  };
}

Using it instead of remotedev decorator in that repo also causes loosing _http value:

@Injectable()
@extendClass
export class Store {

    @observable
    public id: number = 1;

    constructor(
        public _http: Http
    ) {
        console.log(this._http); // it's undefined
    }
}

Let me know if there's something we can do from our side.

@sumitarora
Copy link
Contributor

Thanks @zalmoxisus for investigating

@filipesilva filipesilva reopened this Mar 14, 2017
@Jonatthu
Copy link
Author

@sumitarora So this has a possible solution on angular-cli webpack config or it needs another external solution?

@sumitarora
Copy link
Contributor

@Jonatthu Will have to investigate what is exactly causing this issue.

@hansl hansl added this to the 1.0 Final Blocking milestone Mar 16, 2017
@hansl hansl added P5 The team acknowledges the request but does not plan to address it, it remains open for discussion and removed P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful labels Mar 16, 2017
@hansl hansl modified the milestones: 1.0 Final Blocking, 1.0 Final NON-Blocking Mar 16, 2017
@sumitarora
Copy link
Contributor

@Jonatthu After investigation current behavior is expected and appropriate. Extending class is decorator is not supported as we do don't support class-level decorators.
Using extend we lost the type information which causes this behavior and gets everything undefined.

@hansl
Copy link
Contributor

hansl commented Mar 21, 2017

You can also refer to this issue for a similar use case (inheriting from a function call): angular/angular#13436

@Jonatthu
Copy link
Author

@sumitarora @zalmoxisus Is there another way to make it work with angular knowing this?
#4994 (comment)

@bhaidar
Copy link

bhaidar commented Aug 6, 2018

Is there a solution in here? My case is with a Class Decorator. Once I added that to a Component, all dependencies are undefined now.

Thanks

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs: investigation Requires some digging to determine if action is needed P5 The team acknowledges the request but does not plan to address it, it remains open for discussion type: bug/fix
Projects
None yet
Development

No branches or pull requests

6 participants