我对angular并不陌生,在定义角度子路线时遇到一些问题。我不知道我在做什么错。为子组件创建不同的模块时,在定义路由时会产生问题。
Template parse errors: 'app-sidebar' is not a known element: 1. If 'app-sidebar' is an Angular component, then verify that it is part of this module. 2. If 'app-sidebar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
<h1>Welcome Admin!!</h1>
<ul>
<li>[ERROR ->]<app-sidebar></app-sidebar></li>
<li><router-outlet></router-outlet></li>
</ul>`
这是项目的 structure
这是 main module
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule,Routes } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from "@angular/http";
import { AdminModule } from './admin/admin.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { MainpageComponent } from './mainpage/mainpage.component';
import { AdminComponent } from './admin/admin.component';
const appRoutes: Routes = [
{
path: '',
component: MainpageComponent
},
{
path: 'admin',
component: AdminComponent
}
];
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
MainpageComponent,
AdminComponent,
],
imports: [
BrowserModule,
AdminModule,
RouterModule.forRoot(appRoutes),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
这是 admin module
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule,Routes } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from "@angular/http";
import { SidebarComponent } from './sidebar/sidebar.component';
import { BookingComponent } from './booking/booking.component';
const adminRoutes: Routes = [
{
path: 'booking',
component: BookingComponent
}
];
@NgModule({
imports: [
BrowserModule,
CommonModule,
RouterModule.forChild(adminRoutes),
],
declarations: [
SidebarComponent,
BookingComponent,
],
exports: [
RouterModule
]
})
export class AdminModule { }
这是 admin.component.html
中出现问题的html代码
<div class="admin-header">
<h1>Welcome Admin!!</h1>
<ul>
<li><app-sidebar></app-sidebar></li>
<li><router-outlet></router-outlet></li>
</ul>
</div>
为什么sidebar component
路由不起作用?
答案 0 :(得分:0)
导入模块的顺序也很重要。您需要在AdminModule
之后导入RouterModule.forRoot()
,然后在AdminModule中定义一条路由来加载SideBarComponent。