gpt4 book ai didi

Angular2 路由器 : Error: Cannot find primary outlet to load 'InboxComponent'

转载 作者:太空狗 更新时间:2023-10-29 17:00:42 24 4
gpt4 key购买 nike

我创建了一个带路由的简单应用。链接“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() { }

}

inbox.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() {
}

}

最佳答案

你的 MailListComponent需要 <router-outlet>同样,因为 InboxComponent在你的路由器配置中被定义为一个 child :

@Component({
selector: 'app-mail-list',
template: `
<p>
Mail list works!
</p>
<router-outlet></router-outlet>
`,
styleUrls: ['./mail-list.component.css']
})
export class MailListComponent {

constructor() { }

}

如果你想渲染 InboxComponent在同一个导出内,你不应该把它添加为 child

关于Angular2 路由器 : Error: Cannot find primary outlet to load 'InboxComponent' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41847957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com