我用路由创建了一个简单的应用程序 链接" http:// localhost:4200 "和" http:// localhost:4200/mail "运行良好,
但是当我尝试打开一个链接" http:// localhost:4200/mail/inbox "时,我收到一个错误:"找不到加载'InboxComponent'的主要插座".
是什么导致错误,我该如何解决?
这是我的*.ts文件:
app.module.ts:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; import { RouterModule } from '@angular/router'; import { AppComponent } from './app.component'; import { MailListComponent } from './components/mail-list/mail-list.component'; import { FoldersComponent } from './components/folders/folders.component'; import { InboxComponent } from './components/inbox/inbox.component'; const routes = [ { path: '', component: MailListComponent }, { path: 'mail', component: MailListComponent, children: [ {path: 'inbox', component: InboxComponent} ] } ]; @NgModule({ declarations: [ AppComponent, MailListComponent, FoldersComponent, InboxComponent ], imports: [ BrowserModule, FormsModule, HttpModule, RouterModule.forRoot(routes) ], bootstrap: [AppComponent] }) export class AppModule { }
app.component.ts:
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ``, styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'app works!'; }
folders.component.ts:
import { Component } from '@angular/core'; @Component({ selector: 'app-folders', template: ` `, styleUrls: ['./folders.component.css'] }) export class FoldersComponent { folders: any[]; constructor() { } }
mail-list.component.ts:
import { Component } from '@angular/core'; @Component({ selector: 'app-mail-list', template: `Mail list works!
`, styleUrls: ['./mail-list.component.css'] }) export class MailListComponent { constructor() { } }
inbox.component.ts:
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-inbox', template: `inbox works!
`, styleUrls: ['./inbox.component.css'] }) export class InboxComponent implements OnInit { constructor() { } ngOnInit() { } }
PierreDuc.. 23
您的MailListComponent
需求
也是如此,因为InboxComponent
在路由器配置中定义为子项:
@Component({ selector: 'app-mail-list', template: `Mail list works!
`, styleUrls: ['./mail-list.component.css'] }) export class MailListComponent { constructor() { } }
但是,如果您想InboxComponent
在同一个插座内部渲染,则不应将其添加为儿童
您的MailListComponent
需求
也是如此,因为InboxComponent
在路由器配置中定义为子项:
@Component({ selector: 'app-mail-list', template: `Mail list works!
`, styleUrls: ['./mail-list.component.css'] }) export class MailListComponent { constructor() { } }
但是,如果您想InboxComponent
在同一个插座内部渲染,则不应将其添加为儿童
如果要避免添加a的另一种方法
是将定义路线的方式更改为以下内容:
const routes = [ { path: '', children: [ { path: '', component: MailListComponent } ] }, { path: 'mail', children: [ { path: '', component: MailListComponent }, { path: 'inbox', component: InboxComponent } ] } ];
这将允许您使用与
上面相同的方法来托管新组件