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

ROUTES provider in a standalone lazy-loaded component not providing routes #54518

Closed
Ketec opened this issue Feb 20, 2024 · 3 comments
Closed

Comments

@Ketec
Copy link

Ketec commented Feb 20, 2024

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

Using a provider in the ngModule seems to work fine - the routes in the provider are available.
Converting the component into a standalone component, the same provider no longer functions. The factory isn't even called. Same for when I use route providers.

The fact that the factory is not called seems to indicate that the token was never consumed. Or was consumed before the component initialized? I also cannot use inject() directly in loadChildren as it is not supported.

@Component({
  selector: 'app-dynamic-test',
  standalone: true,
  templateUrl: './test.component.html',
  providers: [
    {
      provide: ROUTES,
      multi: true,
      useFactory: () => {
        console.log('factory called');
        const config = inject(CONFIG);
        return config.routes ?? [];
      }
    }
  ],
  imports: [
    RouterOutlet
  ],
})
export class TestComponent {}

Or in routes.ts

export const routes: Routes = [
  {
    path: '',
    component: TestComponent,
    providers: [
      {
        provide: ROUTES,
        multi: true,
        useFactory: () => {
          console.log('factory called');
          const config = inject(CONFIG);
          return config.routes ?? [];
        }
      }
    ]
  }
];
loadChildren: () => import('./dynamic/routes').then(m => m.routes)

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 15.2.9
Node: 18.18.2
Package Manager: npm 9.8.1
OS: linux x64

Angular: 15.2.9
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, language-service, localize, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1502.9
@angular-devkit/build-angular   15.2.9
@angular-devkit/core            15.2.9
@angular-devkit/schematics      15.2.9
@schematics/angular             15.2.9
ng-packagr                      15.2.2
rxjs                            7.8.1
typescript                      4.9.5

Anything else?

No response

@atscott
Copy link
Contributor

atscott commented Feb 20, 2024

This is working as expected. Routes need to be provided in the environment injector tree. Putting it in the component providers makes it available to the component but not services provided in root.

When migrating form loadChildren that uses modules, you should update your lazy children to just be a dynamic import of the routes array directly. It does not need to be provided anywhere at all. https://angular.io/guide/standalone-components#lazy-loading-many-routes-at-once

@atscott atscott closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2024
@atscott
Copy link
Contributor

atscott commented Feb 20, 2024

If your routes are dynamic and you do need that factory then I think what you’re looking for is this #51532

@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 Mar 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants