typescript 错误:“pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pay-pAngular 2

gv8xihay  于 2023-03-04  发布在  TypeScript
关注(0)|答案(3)|浏览(293)

下面是我的文件夹结构。

app
    - common
         - header
           header.component.css
           header.component.html
           header.component.ts
        - footer
           footer.component.css
           footer.component.html
           footer.component.ts
    - index
      index.component.css
      index.component.ts
      index.component.html
      index.module.ts
   - lessons
     lesson1(another folder)
     lesson2(folder)
     lesson.module.ts   

    app.component.css
    app.component.ts
    app.component.html
    app.module.ts

我已经在app. module中导入了页眉页脚组件、index. module、lesson. module并使用了

<app-header></app-header>
<app-navbar></app-navbar>

在index.component.html、课程1.component.html和课程2.component.html中。
但我得到'app-header'不是一个已知的元素错误。有人能帮我解决这个错误吗?
如果我直接在index.module.ts中包含页眉和页脚组件,效果会很好

    • 应用程序模块. ts**
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';
import * as $ from 'jquery';

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

import { HeaderComponent } from './common/header/header.component';
import { FooterComponent } from './common/footer/footer.component';

import { IndexModule } from './index/index.module';
import { LessonModule } from './index/lesson.module';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule,
    IndexModule,
  ],

  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
  ],

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

 }
    • 索引.组件. html**
<app-header></app-header>   <app-navbar></app-navbar>
    • 索引模块ts**
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { IndexComponent } from './index.component';
import { IndexRoutingModule } from './index-routing.module';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    HttpModule,
    IndexRoutingModule
  ],
  declarations: [
    IndexComponent
  ]
})

export class IndexModule { }
    • 课程模块ts**
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { Lesson1Component } from './lesson1.component';
import { Lesson2Component } from './lesson2.component';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    HttpModule,
    IndexRoutingModule
  ],
  declarations: [
    IndexComponent
  ]
})

export class IndexModule { }
ocebsuys

ocebsuys1#

加载应用程序的引导模块是什么?

如果你想在一个模块中声明组件,并在另一个模块中使用它们,你需要export它们,这样当你在另一个模块中导入该模块时,它会理解这些组件将从另一个模块中使用
因此在app.module.ts中声明并导出它们,以便索引模块应该理解这些来自其他模块。

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule,
    IndexModule,
  ],

  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
  ],

exports: [
      HeaderComponent,
    FooterComponent,
],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {

 }

现在导入索引模块中的app.module

@NgModule({
  imports: [
    AppModule,
    CommonModule,
    FormsModule,
    HttpModule,
    IndexRoutingModule
  ],
  declarations: [
    IndexComponent
  ]
})

export class IndexModule { }

我会说,使您的应用程序模块作为一个引导模块,使一个共享模块,并声明所有组件,管道,指令和导出它们。并在您的应用程序模块导入共享模块,并使用所有组件。

rjee0c15

rjee0c152#

您在app.module中导入并声明了头文件组件,但在index.module中使用了它,在那里它们无法被“识别”。
将这些移动到索引模块

import { HeaderComponent } from './common/header/header.component';
...
declarations: [
    HeaderComponent,
....
ehxuflar

ehxuflar3#

尝试添加导出列表,您声明的组件。
示例;
@NgModule({声明:[页眉组件,侧边栏组件,页脚组件],
导入:[公用模块]
,导出:[页眉组件,侧边栏组件,页脚组件]

相关问题