Skip to content

Unclear resolution for "The component must be standalone" #48945

Closed

Description

@eternalmatt

Describe the problem that you experienced

This error is printed in the console and can be more clear.

See the assertStandalone function:
https://github.com/angular/angular/blob/main/packages/router/src/utils/config.ts

The error told me to "Invalid configuration of route 'XYZ' The component must be standalone"

However, this would NOT fix my problem. The issue was not that my component MUST be standalone. Rather, I was using it in a non-standalone situation.

Enter the URL of the topic with the problem

https://github.com/angular/angular/blob/main/packages/router/src/utils/config.ts

Describe what you were looking for in the documentation

Describe in more detail 'standalone'. Perhaps say "This component is trying to be loaded as standalone but it is not marked as standalone. Did you mean to include it in a module?"

Describe the actions that led you to experience the problem

My issue is that I incorrectly used loadComponent instead of loadChildren which is an obvious mistake. However I likely did this because the two properties are named similar and did not present any type-safety problems.

Describe what you want to experience that would fix the problem

Better error messaging. Type Safety would be best.

Add a screenshot if that helps illustrate the problem

No response

If this problem caused an exception or error, please paste it here

{
    path: 'protected',
    canLoad: [OktaAuthGuard],
    canActivate: [OktaAuthGuard],
    loadComponent: () => import('./protected/protected.module').then((m) => m.ProtectedModule),
  },


### If the problem is browser-specific, please specify the device, OS, browser, and version

_No response_

### Provide any additional information here in as much as detail as you can

_No response_

Activity

  1. atscott commented on Feb 3, 2023

    @atscott
    Contributor

    The reason it's not a type error is that modules and components are both classes. We can't distinguish between the two in the type system. That said, the runtime check can likely be improved.

  2. added
    P4
    open for contributions
    on Feb 3, 2023
  3. added this to the Backlog milestone on Feb 8, 2023
  4. IvanNavarroCode commented on Feb 19, 2023

    @IvanNavarroCode
    Contributor

    @atscott maybe the message could be improved like this? Any suggestion is appreciated.

    Invalid configuration of route 'XYZ'. The component must be standalone. Maybe you meant to use loadChildren instead of loadComponent if you are loading a module or you are missing the standalone: true flag in your Component decorator.

    I could submit a PR if this looks OK.

  5. atscott commented on Feb 19, 2023

    @atscott
    Contributor

    @Ivnosing I was thinking that instead you can look at the instance type of the item and see if it's a module to provide a different message that doesn't say "maybe" since now we're pretty sure we know what was meant. That is, something like

    if (component instanceof NgModuleFactory) {
      // new message saying lazy loading a module belongs in loadChildren
    } else if (component && !isStandalone(component)) {
      // ... the old message
    }
    
  6. JeanMeche commented on Apr 11, 2023

    @JeanMeche
    Member

    @atscott I think this issue can be closed now !

  7. angular-automatic-lock-bot commented on May 12, 2023

    @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.

  8. locked and limited conversation to collaborators on May 12, 2023
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

    P4area: routerhotlist: error messagesopen for contributionsrouter: lazy loading

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions