Angular的延迟加载,找不到组件

时间:2019-07-17 08:31:57

标签: javascript angular pdf-viewer

我在6中拥有一个Angular应用程序,并且尝试使Lazy加载正常工作。但是,如果我删除了必须延迟加载的模块。然后,应用程序出现错误。

当然会搜索视频,并且在Google上搜索很多。但这是一个具体情况。

因此,负责延迟加载的部分将是文档。

该模块如下所示:

@NgModule({

    imports: [
        RouterModule,
        CommonModule,
        ReactiveFormsModule,
        DossierRoutingModule,
        SharedModule
        ],

    declarations: [
        DossierComponent,
        DossierCorrespondenceComponent,
        DossierCorrespondenceItemComponent,
        DossierEntryComponent,
        DossierEntrySummaryComponent,
        DossierHistoryComponent,
        DossierLabComponent,
        DossierMedicationComponent,
        DossierMiscComponent,
        DossierNavigationComponent,
        DossierPhysicalComponent,
    ],

exports: [
        DossierComponent,
        DossierCorrespondenceComponent,
        DossierCorrespondenceItemComponent,
        DossierEntryComponent,
        DossierEntrySummaryComponent,
        DossierHistoryComponent,
        DossierLabComponent,
        DossierMedicationComponent,
        DossierMiscComponent,
        DossierNavigationComponent,
        DossierPhysicalComponent,
    ],
})

和dossier.routing.module文件如下:

const dossierRoutes = [
  {
    path: '',
    component: DossierComponent,
    canActivate: [AuthGuard],
    children: [
      {path: '', redirectTo: 'voorgeschiedenis', pathMatch: 'full', CanActivate: [AuthGuard]  },
      {path: 'voorgeschiedenis', component: DossierHistoryComponent, CanActivate: [AuthGuard]  },
      {path: 'lichamelijk', component: DossierPhysicalComponent, CanActivate: [AuthGuard]  },
      {path: 'lab', component: DossierLabComponent, CanActivate: [AuthGuard]  },
      {path: 'overig', component: DossierMiscComponent, CanActivate: [AuthGuard]  },
      {path: 'medicatie', component: DossierMedicationComponent, CanActivate: [AuthGuard]   },
      {path: 'correspondentie', component: DossierCorrespondenceComponent, CanActivate: [AuthGuard]  },
    ]
  },
];

@NgModule({
  imports: [RouterModule.forChild(dossierRoutes)],
  exports: [RouterModule]
})

export class DossierRoutingModule {}

然后在app.routes.ts文件中。我这样做:

{path: '', redirectTo: '/dossier', pathMatch: 'full'},
  {path: 'dossier', loadChildren: './dossier/dossier.module#DossierModule' },

但是当我从app.module.ts中删除DossierModule时。 app.module.ts文件现在看起来像这样:

imports: [
    AdviceModule,
    TrainingModule,
    MeasurementModule,
    SettingsAccountModule,
    ObjectiveModule,
    TodoModule,
    PanelModule,
    EcheqModule,
    SharedModule,
    BrowserModule,
    FormsModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    DragulaModule.forRoot(),
    RouterModule,
    ChartsModule,
    PdfViewerModule,
    HttpClientModule
  ],

然后我会收到此错误:

compiler.js:215 Uncaught Error: Template parse errors:
'app-dossier-navigation' is not a known element:
1. If 'app-dossier-navigation' is an Angular component, then verify that it is part of this module.
2. If 'app-dossier-navigation' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<app-dossier-navigation></app-dossier-navigation>
<app-pdf-viewer [pdfTitle]="fileName" (back)="hand"): ng:///AppModule/DossierPdfComponent.html@0:0
    at syntaxError (compiler.js:215)
    at TemplateParser.push../node_modules/@angular/compiler/fesm5/compiler.js.TemplateParser.parse (compiler.js:14702)

谢谢

那我做错了什么?因为我有组件:

DossierNavigationComponent

dossier.module.ts文件中。

这是共享模块:

@NgModule({

    imports: [CommonModule, RouterModule, ChartsModule],

    // tslint:disable-next-line: max-line-length
    declarations: [
       IsLoadingComponent,
       MeasurementNavigationComponent,
       ModalComponent, TopbarComponent,
       MeasurementGraphComponent,
       ResourceItemComponent,
       Vital10PageComponent,
       VpointStickerComponent
      ],

    // tslint:disable-next-line: max-line-length
    exports: [
      IsLoadingComponent,
      MeasurementNavigationComponent,
      ModalComponent, TopbarComponent,
      MeasurementGraphComponent,
      ResourceItemComponent,
      Vital10PageComponent,
      VpointStickerComponent
        ]

})

export class SharedModule {}

哦,我仍然收到此错误:

Uncaught Error: Template parse errors:
Can't bind to 'pdfTitle' since it isn't a known property of 'app-pdf-viewer'.
1. If 'app-pdf-viewer' is an Angular component and it has 'pdfTitle' input, then verify that it is part of this module.
2. If 'app-pdf-viewer' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("cklink]="{url: '/informatie', label: 'Terug'}"></app-topbar>
<app-pdf-viewer [hidden]="!pdfLoaded" [ERROR ->][pdfTitle]="brochureName" (back)="handlePdfBack()" >
</app-pdf-viewer>
"): ng:///AppModule/BrochureDetailComponent.html@1:38
'app-pdf-viewer' is not a known element:
1. If 'app-pdf-viewer' is an Angular component, then verify that it is part of this module.
2. If 'app-pdf-viewer' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("<app-topbar [backlink]="{url: '/informatie', label: 'Terug'}"></app-topbar>
[ERROR ->]<app-pdf-viewer [hidden]="!pdfLoaded" [pdfTitle]="brochureName" (back)="handlePdfBack()" >
</app-pdf"): ng:///AppModule/BrochureDetailComponent.html@1:0
    at syntaxError (compiler.js:215)

所以我将组件:PdfViewerComponent移到了dossier.module,就像这样:

 declarations: [
        DossierComponent,
        DossierCorrespondenceComponent,
        DossierCorrespondenceItemComponent,
        DossierEntryComponent,
        DossierEntrySummaryComponent,
        DossierHistoryComponent,
        DossierLabComponent,
        DossierMedicationComponent,
        DossierMiscComponent,
        DossierNavigationComponent,
        DossierPhysicalComponent,
        DossierPdfComponent,
        PdfViewerComponent

但这没有帮助。

'app-pdf-viewer'看起来像这样:

export class PdfViewerComponent implements OnInit, AfterViewInit, OnDestroy {
  pdfUrl: string;
  @Input() pdfTitle: string;
  @Output() back: EventEmitter<boolean> = new EventEmitter();
  pdfReadyProxy: any;
  selectedPage: number;
  numPages: number;
  zoom: number;
  @ViewChild(PdfViewerControlsComponent) controls: PdfViewerControlsComponent;
  ng2pdfContainerElement: any; // already a native element
  pdfViewerElement: any; // already a native element
  pdfViewerWrapperElement: any;
  pdfRendered = false;
  runningInApp = false;

2 个答案:

答案 0 :(得分:2)

您的pdf查看器模块(没有延迟加载)具有用于文档导航的html元素。

不知道解决此问题的最简单方法是什么,因为我不知道完整的用例和用户流程。

答案 1 :(得分:0)

如果在DossierNavigationComponent AppModule中使用了DossierModule,则应在两个模块中都声明它。实现此目的的一个好模式是创建一个SharedModule并将DossierModule保留在其declarations中(以及其他类似组件)并添加到{{1}的imports }和AppModule

相关问题