简体   繁体   English

Angular 7:Angular 路由错误 **'router-outlet'** 不是已知元素:

[英]Angular 7 :Error with Angular routing **'router-outlet'** is not a known element:

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.我已经生成了四个不同的commonents Menu、home、contact和about。此外还有Header组件,稍后将帮助包含memu栏以在不同组件之间切换。还有页脚组件。app-routing.ts和 routes.ts 文件也附在这个文件中,这有助于路由目的。

'router-outlet' is not a known element: 1. If 'router-outlet' is an Angular component, then verify that it is part of this module. 'router-outlet'不是已知元素: 1. 如果 'router-outlet' 是 Angular 组件,则验证它是该模块的一部分。 2. If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the 2. 如果“router-outlet”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到

'@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路线.ts文件

 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 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 { }

app.module.ts file app.module.ts文件


  
  
  
    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.component.html文件

 <app-header></app-header> <router-outlet></router-outlet> <app-footer></app-footer>

I see you are missing app-routing.module.ts inside the imports of AppModule我看到您在 AppModule 的导入中缺少 app-routing.module.ts

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

})
export class AppModule{ }

I noticed that in your app.module.ts file you have back ticks ``` in inappropriate places somewhere around providers & bootstrap array.我注意到在您的app.module.ts文件中,您在providersbootstrap数组周围的不适当位置有反引号 ```。 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.这样做:删除 app.module.ts 文件中的所有反引号,就像我在下面所做的那样,然后重新启动您的终端。 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.这是下面 gif 中的结果,路由正常工作且控制台中没有错误。 Example with just home & menu components.仅包含主页和菜单组件的示例。

Final result:最后结果:

显示路线工程的 Gif

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:文件夹结构:

在此处输入图像描述

And here are the contents of my files:这是我的文件的内容:

route.ts file: route.ts 文件:

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: app.module.ts 文件:

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: app.component.ts 文件:

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

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

}

app-routing.module.ts:应用程序路由.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: menu.component.ts:

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

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

}

home.component.ts: 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.就像我说的,一旦我从 app.module.ts 中删除了那些不必要的反引号,你的代码就可以正常工作,没有错误。

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.我从头开始一切。然后我的问题得到了解决。我不完全知道我的问题在哪里。但是,可能我已经在单独的文件夹中制作了路由文件。在 app-routing 文件夹中制作路由文件,解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM