You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Issue with assigning lazy loaded standalone components environment injectors #50724
I’ve logged the injectors created by each component as they render, it looks like the first Lazy component that gets loaded is the one that gets a new Environment Injector, the second does not, causing the DI error. We get an R3 injector for App and 1 for either LazyComponent or LazyTwoComponent, based on which loads first. I think this is related to the usage of loadComponent
This explains why the order of component activation matters for your reproduction. When the second component is created, it's pulling the injector from the cache instead of creating a new one like you'd expect.
Thnx for digging into it @garrettld - this raises an interesting question of using component's id as a key in the cachedInjectors - it seems like it is not reliable enough.
this raises an interesting question of using component's id as a key in the cachedInjectors - it seems like it is not reliable enough.
@pkozlowski-opensource agreed, the component ids that we generate based on the metadata may have collisions. We plan to improve their generation further (by using more fields, including styles), but we should transition to something else (probably a WeakMap) in the DI code to always have proper component->injector connection. // cc @alan-agius4
Activity
garrettld commentedon Jun 15, 2023
I've created a minimal reproduction that doesn't use lazy-loaded components or the router here: https://stackblitz.com/edit/angular-extdt1?file=src%2Fmain.ts
The issue is that your components have the same selector. I'm not sure why this causes this behavior (edit: see my comment below for why), but you can see the NG0912 warning (https://angular.io/errors/NG0912) in the console.
The issue goes away if you fix the duplicate component id issue in any of the ways described by the NG0912 docs.
garrettld commentedon Jun 15, 2023
I think the root cause is that standalone component injectors are cached by their component id here:
angular/packages/core/src/render3/features/standalone_feature.ts
Lines 26 to 41 in 4550fe4
This explains why the order of component activation matters for your reproduction. When the second component is created, it's pulling the injector from the cache instead of creating a new one like you'd expect.
pkozlowski-opensource commentedon Jul 5, 2023
Thnx for digging into it @garrettld - this raises an interesting question of using component's id as a key in the
cachedInjectors- it seems like it is not reliable enough.pkozlowski-opensource commentedon Jul 5, 2023
Related: #46093 and #50158
AndrewKushnir commentedon Jul 5, 2023
@pkozlowski-opensource agreed, the component ids that we generate based on the metadata may have collisions. We plan to improve their generation further (by using more fields, including
styles), but we should transition to something else (probably aWeakMap) in the DI code to always have proper component->injector connection. // cc @alan-agius4fix(core): ensure that standalone components get correct injector ins…
fix(core): ensure that standalone components get correct injector ins…
fix(core): ensure that standalone components get correct injector ins…
angular-automatic-lock-bot commentedon Aug 10, 2023
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.
fix(core): ensure that standalone components get correct injector ins…
fix(core): ensure that standalone components get correct injector ins…