0

I have tried to include all imports to the necessary files,But I have got the following error while compiling. How could I solve it?

I have generated four differnet commonents Menu,home,contact,and about.Additional to this thare are also Header component that will later help to contain the memu bar to switch between the differnt components.And also footer component.The app-routing.ts and routes.ts files are also attached with this file,which helps for routing purpose.

'router-outlet' is not a known element: 1. If 'router-outlet' is an Angular component, then 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' of this component to suppress this message. ("<app-header></app-header>
    [ERROR ->]<router-outlet></router-outlet>
    <app-footer></app-footer>"): ng:///AppModule/AppComponent.html@1:0
        at syntaxError (compiler.js:1021)
        at TemplateParser.push../node_modules/@angular/compiler/fesm5      /compiler.js.TemplateParser.parse (compiler.js:14830)
        at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._parseTemplate (compiler.js:24018)
        at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileTemplate (compiler.js:24005)
        at compiler.js:23948
        at Set.forEach (<anonymous>)
        at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileComponents (compiler.js:23948)
        at compiler.js:23858
        at Object.then (compiler.js:1012)
        at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents (compiler.js:23857)


      

routes.ts file

 

 

      import { Routes } from '@angular/router';
        
        import { MenuComponent } from '../menu/menu.component';
        import { DishdetailComponent } from '../dishdetail/dishdetail.component';
        import { HomeComponent } from '../home/home.component';
        import { AboutComponent } from '../about/about.component';
        import { ContactComponent } from '../contact/contact.component';
        
        export const routes: Routes = [
          { path: 'home',  component: HomeComponent },
          { path: 'menu',     component: MenuComponent },
          { path: '', redirectTo: '/home', pathMatch: 'full' }
                      ];

app-routing.module.ts file

   

 
            import { NgModule } from '@angular/core';
            import { CommonModule } from '@angular/common';
            import { RouterModule, Routes } from '@angular/router';
            
            import { routes } from './routes';
            
            @NgModule({
              imports: [
                CommonModule,
                RouterModule.forRoot(routes)
              ],
              exports: [ RouterModule ],
              declarations: []
            })
            export class AppRoutingModule { }

app.module.ts file

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    import { MatToolbarModule } from '@angular/material/toolbar';
    import  {MatListModule} from '@angular/material/list';
    import  {MatGridListModule} from '@angular/material/grid-list';
    import  {MatCardModule} from "@angular/material/card";
    import  {MatButtonModule} from "@angular/material/Button";
    import { FlexLayoutModule }  from '@angular/flex-layout';

    import 'hammerjs';

    import { AppComponent } from './app.component';
    import { MenuComponent } from './menu/menu.component';
    import { FooterComponent} from './footer/footer.component';
    import { HeaderComponent} from './header/header.component';
    import { DishdetailComponent } from './dishdetail/dishdetail.component';
    import { HomeComponent } from  './home/home.component'
    import { AboutComponent } from './about/about.component';
    import { ContactComponent } from './contact/contact.component';

    import {DishService} from './services/dish.service';

    import {RouterModule} from '@angular/router';
    import { AppRoutingModule } from './app-routing/app-routing.module';
         @NgModule({
                  declarations: [
                  AppComponent,
                  MenuComponent,
                  DishdetailComponent,
                  HeaderComponent,
                  FooterComponent,
                  HomeComponent,
                  AboutComponent,
                  ContactComponent
                 ],
     imports: [
        BrowserModule,
        BrowserAnimationsModule,
        FlexLayoutModule,
        MatToolbarModule,
        MatGridListModule,
        MatCardModule,
        MatButtonModule,
        MatListModule,
        AppRoutingModule,
        RouterModule
      ],` 
      `providers: [
        DishService
      ],`
     `bootstrap: [AppComponent]` 
   `})` 

    export class AppModule { }

app.component.html file

   

<app-header></app-header>
<router-outlet></router-outlet>
<app-footer></app-footer>
Bansi29
  • 1,053
  • 6
  • 24
Petros Belachew
  • 37
  • 1
  • 10

3 Answers3

0

I see you are missing app-routing.module.ts inside the imports of AppModule

@NgModule({
  imports: [
    CommonModule,
    routingModule
  ],

})
export class AppModule{ }
0

I noticed that in your app.module.ts file you have back ticks ``` in inappropriate places somewhere around providers & bootstrap array. I copied & pasted this from your own code so you can see it better:

      ...MatListModule,
        AppRoutingModule,
        RouterModule
      ],` 
      `providers: [
        DishService
      ],`
     `bootstrap: [AppComponent]` 
   `})` 

    export class AppModule { }

Do this: Remove all the back ticks in the app.module.ts file like I have done below and restart your terminal. This should solve any problems you are having.

      ...MatListModule,
        AppRoutingModule,
        RouterModule
      ],
      providers: [
        DishService
      ],
     bootstrap: [AppComponent]
   })

    export class AppModule { }

I copied and pasted your code and worked on the files you provided information about. With the back ticks removed, your code works fine here on my laptop. Here's the result in the gif below, route works properly & no errors in the console. Example with just home & menu components.

Final result:

Gif showing route works

I removed all other things that didn't relate to routing from your code so that it would be easier to detect errors. Here's my folder structure.

Folder structure:

enter image description here

And here are the contents of my files:

route.ts file:

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

import { MenuComponent } from './menu/menu.component';
import { HomeComponent } from './menu/home.component';

export const routes: Routes = [
  { path: 'home',  component: HomeComponent },
  { path: 'menu',     component: MenuComponent },
  { path: '', redirectTo: '/home', pathMatch: 'full' }
];

app.module.ts file:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { MenuComponent } from './menu/menu.component';
import { HomeComponent } from './menu/home.component';
import { AppRoutingModule } from './app-routing.module';
import { RouterModule } from '@angular/router';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    MenuComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    RouterModule
  ],
  providers: [

  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts file:

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

@Component({
  selector: 'app-root',
  template: `
    <router-outlet></router-outlet>
  `
})
export class AppComponent {

}

app-routing.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';

import { routes } from './routes';

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

menu.component.ts:

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

@Component({
  template: '<h1>Menu Component</h1>'
})
export class MenuComponent {

}

home.component.ts:

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

@Component({
  template: '<h1>Home Component</h1>'
})
export class HomeComponent {

}

Like I said, once I removed those unnecessary back ticks from the app.module.ts, your code worked fined with no errors.

Mary Obiagba
  • 185
  • 1
  • 12
  • which angular version are you working on? I am working on angular 11 and facing the same issue. Please help if you get a solution – Dhritiman Tamuli Saikia Feb 05 '21 at 13:31
  • I am working on Angular 7.And still couldn't able to solve the issue. – Petros Belachew Feb 05 '21 at 13:40
  • I have updated the answer. Petros, please check it again. Dhritiman, can you share the link to your question here in the comments? – Mary Obiagba Feb 05 '21 at 14:45
  • Normally back ticks are typo errors,they doesn't exist in my code.But,If the code just worked for you,I will create the new app and check it.Than you! – Petros Belachew Feb 06 '21 at 13:07
  • Where is your 'app.component.html' file? – Petros Belachew Feb 06 '21 at 13:42
  • Oh. I see. Ok... I didn't create a app.component.html file. I just used a direct template in the app.component.ts file since the code there isn't much. Same for the home & menu components too. You can choose to create a separate html file if you prefer it. – Mary Obiagba Feb 06 '21 at 15:28
  • Yeah, maybe making a new app to test just for routing like I have done above will help. It can be really frustrating to get stuck on one thing for days or weeks. I hope you get to the root of the problem soon. Just know whenever you find what the issue is, you get stronger and better at tracking bugs/issues. – Mary Obiagba Feb 06 '21 at 15:32
  • 1
    Thank you!You have helped me to figure the issue.I was stuck for a weeks,but starting everything from scratch helped me fix the issue. – Petros Belachew Feb 15 '21 at 08:06
  • Yay! Glad to know it's now solved. If you haven't deleted the old project, you can find time to try compare the old & the new. Probably you may find what caused the issue. – Mary Obiagba Feb 15 '21 at 12:23
0

I have started everything from scratch.Then my issue was fixed.I don't exactly know where is my problem.But,probably I have made routes file in separate folder.Making routes file in app-routing folder, fixed my issue.

Petros Belachew
  • 37
  • 1
  • 10