Skip to content

inject and directiveInject need bridging when used with Component.provides #23330

Closed

Description

@mhevery

Currently we generate inject for @Injectable and directiveInject for @Component and @Directive. This works fine since @Injectable declared as part of @NgModule does not need to see anything from the Element Injector Tree.

However this does not work in the case when @Injectable is used with @Component.providers (or @Component.viewProviders) because in that case the @Injectable can see into the Element Injector Tree.

Possible fixes are:

Mark services in @Component.providers using inject (:thumbsdown:)

non-starter as it breaks locality.

Have only one inject (:thumbsdown:)

This would require merging directiveInject into inject

Pros:

  • Easy to implement

Cons:

  • di will have to know about render3
  • Breaks tree shaking since any @Injectable will be pulling in at least some code of render3.

Have ComponentDefFeature which would patch inject to act like directiveInject only when @Component.providers are used (:thumbsup:)

Pros:

  • Tree shaking works as expected.
  • di does not need to know about render3 (needs to have a hook, see cons)

Cons:

  • di has to have a hook into which external render3 can connect in order to redirect inject into directiveInject. Since this is a private hook, it should be small cost.

Syntax

@Inejctable()
class SomeService {
  constructor(dep: SomeDep) {}
  static ngInjectableDef = defineInjectable({
   factory: () => new SomeService(inject(SomeDep));
  });
}

@Component({
  ...,
  provides: [SomeService]
})
class MyComponent {
  static ngComponentDef = defineCompoent({
    ...,
    provides: [SomeService],
    features: [ComponetProvidersFeature]
  });
}

ComponetProvidersFeature would than use some private API to patch the inject to go through the directiveInject in cases where the factory is called as a consequence of @Component.providers.

Activity

  1. added this to the needsTriage milestone on Apr 11, 2018
  2. added
    featureLabel used to distinguish feature request from other issues
    on Apr 11, 2018
  3. modified the milestones: needsTriage, Backlog on Apr 11, 2018
  4. mhevery commented on Nov 18, 2020

    @mhevery
    ContributorAuthor

    This has been resolved.

  5. angular-automatic-lock-bot commented on Dec 19, 2020

    @angular-automatic-lock-bot

    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.

  6. locked and limited conversation to collaborators on Dec 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecore: difeatureLabel used to distinguish feature request from other issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions