Home > OS >  Error when trying to load a "standalone" component from a module
Error when trying to load a "standalone" component from a module

Time:11-04

I want to inject new standalone component CrashReportsComponent into my project. But I get an error when I try to connect a component from module-routing that the component type is not like NgModule NgModule 'CrashReportsComponent' is not a subtype of 'NgModuleType'.

As planned by the developers, I can connect a standalone component instead of a module, and this should work without any changes in the module.

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CommonLayoutComponent } from './common-layout.component';
import { FilterKind } from '../core/services/filter.service';

const routes: Routes = [
    {
        path: '',
        component: CommonLayoutComponent,
        children: [
            {
                path: 'crash-reports',
                loadChildren: () =>
                    import('../pages/crash-reports/crash-reports.component').then((m) => m.CrashReportsComponent),
            },
        ],
    },
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
})
export class CommonLayoutRoutingModule {}
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';

@Component({
    selector: 'app-crash-reports',
    standalone: true,
    imports: [CommonModule],
    templateUrl: './crash-reports.component.html',
    styleUrls: ['./crash-reports.component.scss'],
})
export class CrashReportsComponent {
    constructor() {}
}

CodePudding user response:

You want loadComponent and not loadChildren, I believe.

  • Related