我创建了一个带有路由的简单应用程序。链接“ 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: `
<app-folders></app-folders>
<router-outlet></router-outlet>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}
文件夹.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-folders',
template: `
<aside class="folders">
<ul>
<li><a routerLink="mail/inbox">Inbox</a></li>
</ul>
</aside>
`,
styleUrls: ['./folders.component.css']
})
export class FoldersComponent {
folders: any[];
constructor() { }
}
邮件列表.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-mail-list',
template: `
<p>
Mail list works!
</p>
`,
styleUrls: ['./mail-list.component.css']
})
export class MailListComponent {
constructor() { }
}
收件箱.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-inbox',
template: `
<p>
inbox works!
</p>
`,
styleUrls: ['./inbox.component.css']
})
export class InboxComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}