80

I created routes with deep child paths. I added <router-outlet> to the AdminComponent component which I wrapped into NgModule. But after refreshing the page I got this error:

'router-outlet' is not a known element

Maybe it occurred because I forgot import some module to admin.module.ts

Please help. Thanks.

app.routes.ts

export const routes: Routes = [
    {
        path: '',
        component: AppComponent,
        children: [
            {
                path: '',
                component: LoginComponent
            },
            {
                path: 'admin',
                component: AdminComponent
            },
            {
                path: 'user',
                component: UserComponent
            },
            {
                path: 'there',
                component: ThereComponent
            }
        ]
    }
]

app.module.ts

@NgModule({
    imports: [
        BrowserModule,
        AppRoutes,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        RouterModule,
        TranslateModule.forRoot({
            provide: TranslateLoader,
            useFactory: (http: Http) => {
                return new TranslateStaticLoader(http, './src/assets/i18n', '.json')
            },
            deps: [Http]
        }),
        UserComponentModule,
        AdminComponentModule,
        LoginComponentModule,
        ThereComponentModule,
        DashboardComponentModule
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        FormBuilder
    ],
    bootstrap: [AppComponent]
})

admin.component.ts and admin.module.ts

//  admin.component.ts
import {Component} from "@angular/core";

@Component({
    selector: 'admin',
    template: "<router-outlet></router-outlet>",
})

export class AdminComponent {
    constructor() {

    }
}

//  admin.module.ts
const ADMIN_DECLARATION = [
    AdminComponent
];

@NgModule({
    imports: [
        BrowserModule,
        TranslateModule,
        FormsModule,
        ReactiveFormsModule
    ],
    declarations: [
        ADMIN_DECLARATION
    ],
    exports: [
        ADMIN_DECLARATION
    ],
    providers: [
        TranslateService,
        FormBuilder
    ]
})

export class AdminComponentModule {

}
Joshua Dawson
  • 629
  • 10
  • 17
Stanislav Pyshevskyi
  • 1,072
  • 1
  • 8
  • 12

6 Answers6

88

AdminComponent is part of AdminComponentModule and you have not imported RouterModule inside AdminComponentModule module:

//  admin.component.ts
import {Component} from "@angular/core";

@Component({
    selector: 'admin',
    template: "<router-outlet></router-outlet>",
})

export class AdminComponent {
    constructor() {

    }
}

//  admin.module.ts
const ADMIN_DECLARATION = [
    AdminComponent
];

@NgModule({
    imports: [
        BrowserModule,
        TranslateModule,
        RouterModule,
        FormsModule,
        ReactiveFormsModule
    ],
    declarations: [
        ADMIN_DECLARATION
    ],
    exports: [
        ADMIN_DECLARATION
    ],
    providers: [
        TranslateService,
        FormBuilder
    ]
})

export class AdminComponentModule {

}
yankee
  • 38,872
  • 15
  • 103
  • 162
ranakrunal9
  • 13,320
  • 3
  • 42
  • 43
  • Hi @yankee I am using lazy module feature and I have separated app.routing.module.ts file. if I include 'import { AppRoutingModule } from './app-routing.module'; and import in app.module.ts then again I face https://stackoverflow.com/questions/49670232/routermodule-forroot-called-twice issue. Request you to help if possible ' – Swapnil Yeole Jul 23 '20 at 14:30
46

you did not export the RouterModule.

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
Cengkuru Michael
  • 4,590
  • 1
  • 33
  • 33
7

Add this code

import { provideRoutes} from '@angular/router';

to your app.module.ts

Worked for me.

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
pulkit219
  • 77
  • 1
  • 5
3

In your app.module.ts file

import { routing } from './app-routing.module';

//and then write within imports
@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent
  ],
  imports: [
    BrowserModule,
    **routing**,
    EmployeeModule
  ],
Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
Kumar Abhishek
  • 3,004
  • 33
  • 29
1

This works for me:

Add schema [NO_ERRORS_SCHEMA] in AppModule.

import { NO_ERRORS_SCHEMA } from '@angular/core';

@NgModule({
  schemas : [NO_ERRORS_SCHEMA]  
})
Powkachu
  • 2,170
  • 2
  • 26
  • 36
Kondas Lamar Jnr
  • 143
  • 1
  • 11
-1

app.module.ts

import { MyRoutingModule } from './MyRoutingModulePath';

  @NgModule({
  imports: [
    MyRoutingModule
  ])
profimedica
  • 2,716
  • 31
  • 41
  • While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Sep 12 '18 at 08:28
  • There is nothing original in my late answer. I struggled implementing solutions found over the internet. Exosting, confusing and time expensive because of my architectural particularities. I was looking for a short answer to point me to the solution instead of teaching me the process. After I finally managed it, it became obvious for me what it was difficult to observe in full examples. Sure, there are many ways to fail. But many will fail the same way as me. This answer is for those that are so close to fix it but far from getting it right. – profimedica Sep 12 '18 at 22:25