组件PautasComponent不是任何NgModule或模块的一部分:当实际存在时[Angular 7]

时间:2019-01-16 13:06:59

标签: angular typescript angular7

我收到此错误

ERROR Error: Uncaught (in promise): Error: Component PautasComponent is not part of any NgModule 
or the module has not been imported into your module.
Error: Component PautasComponent is not part of any NgModule or the module has not been imported into your module.
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._createCompiledHostTemplate

问题是昨天我遇到了同样的问题,所以今天我创建了一个新模块,同样的事情发生了。

文件夹结构:

app-routing.ts/
app.component.ts
app.module.ts
  |--features/
      |--features-routing.module.ts
      |--features.module.ts
      |--features.components.ts
      |--pautas/
         |--pautas.component.ts
      |--estudiantes/
         |-- estudiantes.component.ts
  |--shared/
      |--components/
        |--navbar/
          |-- navbar.component.ts
        |--sidebar/
          |-- sidebar.component.ts

在app.component.ts中只有一个<router-outlet></router-outlet>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
// Feature routing modules
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  declarations: [
    AppComponent,
    RegisterComponent,
],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    ReactiveFormsModule,
    CoreModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const appRoutes: Routes = [
  // Auth Module
  {
    path:'login',
    loadChildren: './auth/auth.module#AuthModule'
  },
  {
    path: 'features',
    loadChildren: './features/features.module#FeaturesModule'
  },
  {
    path:'',
    redirectTo: '/login',
    pathMatch: 'full'
  }
]

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

features.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { FeaturesRoutingModule } from './features-routing.module';
import { FeaturesComponent } from './features.component';
import { ComponentsModule } from '../shared/components/components.module';
import { PautasComponent } from './components/pautas/pautas.component';
import { EstudiantesComponent } from './components/estudiantes/estudiantes.component';

@NgModule({
  declarations: [FeaturesComponent, PautasComponent, EstudiantesComponent],
  imports: [
    CommonModule,
    ComponentsModule, // This is from the shared folder
    FeaturesRoutingModule
  ]
})
export class FeaturesModule { }

在这里您可以看到pautas.component已声明

features.routing.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { FeaturesComponent } from './features.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { PautasComponent } from '../components/pautas/pautas.component';
import { EstudiantesComponent } from './components/estudiantes/estudiantes.component';

const routes: Routes = [
  {path: '', component: FeaturesComponent, children: [
      { path: 'dashboard', component: DashboardComponent},
      { path: 'estudiantes', component: EstudiantesComponent},
      { path: 'pautas',   component: PautasComponent },
      { path: '', redirectTo: 'dashboard'}
    ]
  },
];

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

我认为创建新模块可以解决问题,但事实并非如此。而且它仅在pautas.component 中发生。因为当我在features-routing.module.ts中注释掉pautas路线时,我没有出现错误,并且estudiantes.component.ts呈现完美(这确实是奇怪的行为) )。

当我从sidebar.component中[routerLink]="['pautas']"或[routerLink] =“ ['estudiantes']”时都呈现。因此,他们基本上是双胞胎,但其中之一根本不起作用。

1 个答案:

答案 0 :(得分:2)

路径中有两个点。删除一个。在功能路由模块中,组件的路径不正确

import { PautasComponent } from '../components/pautas/pautas.component';