这是我的文件夹:
-app
-content
-footer
-header
-main-header
{other services - shortcut} <- I just shortcut this bec. it is too long.
main-header.component.html
main-header.component.scss
main-header.component.spec.ts
main-header.module.ts
main-header.routing.ts
app.component.html
etc..
主标题模块
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MainHeaderComponent } from './main-header.component';
import { MainHeaderRouteModule } from './main-header.routing';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatCardModule } from '@angular/material/card';
import { MatSidenavModule } from '@angular/material/sidenav';
import { FormsModule } from '@angular/forms';
import { LayoutModule } from '@angular/cdk/layout';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatListModule } from '@angular/material/list';
@NgModule({
declarations: [
MainHeaderComponent
],
imports: [
MatCardModule,
MatCheckboxModule,
MatSidenavModule,
FormsModule,
LayoutModule,
MatToolbarModule,
MatButtonModule,
MatIconModule,
MatListModule,
MatCardModule,
MainHeaderRouteModule,
CommonModule
]
})
export class MainHeaderModule {}
main-header.routing
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MainHeaderComponent } from './main-header.component';
const routes: Routes = [
{path: '', component: MainHeaderComponent}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class MainHeaderRouteModule {}
应用程序模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MainHeaderComponent } from './header/main-header/main-header.component';
@NgModule({
declarations: [
AppComponent,
MainHeaderComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule
],
exports: [
MainHeaderComponent
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.routing.module
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{path: '', redirectTo: 'main-header', pathMatch: 'full'},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
由于我使用的是延迟加载路由,因此主模块似乎无法识别倾斜材料:
'mat-sidenav-container'不是一个已知元素: 1.如果“ mat-sidenav-container”是Angular组件,则请验证它是否是此模块的一部分。 2.如果“ mat-sidenav-container”是Web组件,则将“ CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“ @ NgModule.schemas”以禁止显示此消息。 (“ [错误->]
答案 0 :(得分:0)
我看不到您在app.module中的任何地方导入MainHeaderModule
答案 1 :(得分:0)
根据Lys答案的注释,您需要在app.module.ts中导入MainHeaderModule。然后,您将得到您提到的错误
ERROR in Type MainHeaderComponent in D:/Angular/proj/src/app/header/main-header/main-header.component.ts is part of the declarations of 2 modules
错误是因为在两个模块中都声明了MainHeaderComponent。从app.module.ts中删除MainHeaderComponent。因为它已经在主头模块中声明了。在app.module.ts中导入主标头模块将负责MainHeaderComponent。