router-outlet is not a known element

How to fix router-outlet is not a known element!

First, before fixing that famous error, we want to define and go a little in-depth with router-outlet, the definition why we use it, the error of router-outlet is not a known element! and how to fix it.

router-outlet is not a known element

What is router-outlet (Routing outlet) in angular?

router outlet or router-outlet is a built-in directive in Angular that helps choose the appropriate component to display on the screen.

router-outlet is not a known element

Now, we will move to the famous error. While adding the router outlet directive in the needed HTML emplacement, sometimes you may encounter this error, telling you that router-outlet is not a known element.

let’s take a look at how to add a router outlet directive.

In that way, Angular will be responsible to take the active route and displaying the appropriate component. But, Sometimes an error in the console may appear telling that “router-outlet is not a known element“.

The causes behind this error are multiple. So we will choose the repetitive ones.

Not importing the RouterModule which is responsible for calling the router outlet.

Be careful, if you have a separate routing file you must declare the RouterModule and exports it, or import it directly into the app module file.

This error may occur while working with lazy loading modules , So be wise when you take the solution provided below.

Solution :

router outlet is not a known element unit test

Yes, this error may appear while working in unit testing.

Solution

You should so import RouterTestingModule into your app.component.spec.ts like this :

Conclusion

There are many reasons behind getting router-outlet is not a known element and we tried to cover the most common cases and the ways to solve them, but generally, it’s by importing the RouterModule.

First, before fixing that famous error, we want to define and go a little in-depth with router-outlet, the definition why we use it, the error of router-outlet is not a known element! and how to fix it. What is router-outlet (Routing outlet) in angular? router outlet or router-outlet is a…

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest