I can render a component using routes without having to declare that component in ngModule?

Discussão em 'Angular' iniciado por Hoi Tran Van, Agosto 27, 2025.

  1. Hoi Tran Van

    Hoi Tran Van Guest

    I created a separate component, then added it to the routes array, then I ran the url and saw that the component still rendered in as usual without having to declare that component in any ngModule. The angular version I'm using is v17 and as far as I know, when ng build and ng serve, the routes will work differently.

    //app-routing.module.ts


    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    import { LoggerComponent } from './no-stand-alone/logger.component';
    import { NoStandAlone1Component } from './no-stand-alone/no-stand-alone-1/no-stand-alone-1.component';
    import { NoModuleComponent } from './no-module/no-module.component';

    const routes: Routes = [
    {
    path: "no-module",
    component: NoModuleComponent
    }
    ];

    @NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
    })
    export class AppRoutingModule { }

    ------------------------------------------------------------------------------------
    //no-module.component.ts


    import { Component } from "@angular/core";

    @Component({
    standalone: false,
    selector: "app-no-module",
    template: `<h1>active no module</h1>`,
    styles: []
    })
    export class NoModuleComponent{}

    Continue reading...

Compartilhe esta Página