9

I get the following error on a fresh install of angular6 using the angular-cli.

Uncaught Error: Template parse errors: 'router-outlet' is not a known element: 1. If 'router-outlet' is an Angular component, then verify that it is part of this module.

I followed this guide: https://medium.com/@meemo_86/good-article-beeman-490eaf1399a

And then I followed up on the comment on that article which says to use <router-outlet></router-outlet> instead of <ng-content></ng-content>.

I do those corrections, then I read up on https://angular.io/tutorial/toh-pt5, and do what is stated there.

So now I have a AppRoutingModule, I import that module in app.module.ts, where I also const the routes and added RouterModule.forRoot(appRoutes) to imports.

But I cannot get this error to go away. What am I doing wrong? When I add <router-outler></router-outlet> in my layout.component.htm, the app breaks. I have also searched this topic here and tried several changes but nothing seems to work.

Full source code is here:

https://github.com/ekstremedia/angular6

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Terje Nesthus
  • 810
  • 3
  • 12
  • 30

4 Answers4

26

I see you are missing RouterModule inside the imports of UIModule

@NgModule({
  imports: [
    CommonModule,
    RouterModule
  ],
  declarations: [LayoutComponent, HeaderComponent, FooterComponent],
  exports: [LayoutComponent]
})
export class UiModule { }
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 1
    That is correct, and is a working solution. I mark this as correct answer even though SplitterAlex commented it first, but I cannot mark his answer as correct. Thank you anyway Sajeetharan. – Terje Nesthus Jul 26 '18 at 09:30
  • 1
    oh sorry i was checking your code that time :) glad SplitterAlex helped :) – Sajeetharan Jul 26 '18 at 09:31
5

You are getting the error because you are using the router-outlet component in the UIModule and didn't import the RouterModule inside UIModule.

SplitterAlex
  • 2,755
  • 2
  • 20
  • 23
2

I met the same problem too. And I already imported the RouterModule inside UIModule, still doesn't work. But after I restart the app, it worked, no more errors.

So, what you should do is: 1. Import the RouterModule inside UIModule; 2. Restart your app

李岳芸
  • 21
  • 1
0

In the folder app.module.ts what i did was I imported the following

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

also don't forget to add it in the @NgModule

Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55
user3399358
  • 47
  • 1
  • 2
  • 6