Angluar 报错:Uncaught Error: Template parse errors: ‘router-outlet’ is not a known element:

  • A+
所属分类:Angluar

Angluar 报错:Uncaught Error: Template parse errors: 'router-outlet' is not a known element:

解决方案

运行命令

  1. ng generate module app-routing --flat --module=app  

并更新app-routing.ts文件以添加:

  1. @NgModule({  
  2.   imports: [ RouterModule.forRoot(routes) ],  
  3.   exports: [ RouterModule ]  
  4. })  

这是完整的示例:

  1. import { NgModule }             from '@angular/core';  
  2. import { RouterModule, Routes } from '@angular/router';  
  3.   
  4. import { DashboardComponent }   from './dashboard/dashboard.component';  
  5. import { HeroesComponent }      from './heroes/heroes.component';  
  6. import { HeroDetailComponent }  from './hero-detail/hero-detail.component';  
  7.   
  8. const routes: Routes = [  
  9.   { path: '', redirectTo: '/dashboard', pathMatch: 'full' },  
  10.   { path: 'dashboard', component: DashboardComponent },  
  11.   { path: 'detail/:id', component: HeroDetailComponent },  
  12.   { path: 'heroes', component: HeroesComponent }  
  13. ];  
  14.   
  15. @NgModule({  
  16.   imports: [ RouterModule.forRoot(routes) ],  
  17.   exports: [ RouterModule ]  
  18. })  
  19. export class AppRoutingModule {}  

参考资料

https://stackoverflow.com/questions/44517737/router-outlet-is-not-a-known-element

钰玺

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: