2

I'm trying to use <router-outlet> but getting an error.

Error: 'router-outlet' is not a known element:

  1. If 'router-outlet' is an Angular component, verify that it is part of this module.
  2. If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' component to suppress this message.ng

Angular CLI: 13.3.5 Node: 16.15.0 Package Manager: npm 8.18.0 Visual Studio Code Version: 1.70.2

I checked many other similar questions on StackOverflow but my code seems right.

app.module.ts :

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http'
import { RouterModule } from '@angular/router';
import { SearchBarComponent } from './components/search-bar/search-bar.component';
import { HomeComponent } from './components/home/home.component';

@NgModule({
  declarations: [
    AppComponent,
    SearchBarComponent,
    HomeComponent
  ],
  imports: [
    AppRoutingModule,
    BrowserModule,
    RouterModule,   
    HttpClientModule,      
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './components/home/home.component';

const routes: Routes = [
  {
    path:'',
    component: HomeComponent
  },
  {
    path: 'search/:game-search',
    component: HomeComponent
  }

];

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

app.component.html

<app-search-bar></app-search-bar>
<router-outlet></router-outlet>
Gly
  • 21
  • 4
  • 1
    Does this answer your question? ['router-outlet' is not a known element](https://stackoverflow.com/questions/44517737/router-outlet-is-not-a-known-element) – Dinuda Yaggahavita Aug 31 '22 at 12:31
  • your code seems correct. maybe some caching? have you tried to delete node modules and reinstalling? – Fiehra Aug 31 '22 at 12:38
  • No, i tried all solutions in this link. It didn't work. – Gly Aug 31 '22 at 12:40
  • I will try to re-install node modules – Gly Aug 31 '22 at 12:41
  • Note that you dont need to import `RouterModule` in `AppModule` since you are importing it and exporting it in `AppRoutingModule`. Then `AppModule` imports the `AppRoutingModule`. Maybe this is not a fix to your problem but is important to note. – cybering Aug 31 '22 at 12:43
  • I tried re-install node modules but it didn't work. – Gly Aug 31 '22 at 12:56
  • @Gly Could you try to replicate the issue in a stackblitz for angular13? maybe you will catch what is going wrong – Naren Murali Aug 31 '22 at 13:23

0 Answers0