13

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

}
4

2 回答 2

23

您也MailListComponent需要 a <router-outlet>,因为InboxComponent在路由器配置中定义为子级:

@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在同一插座内渲染,则不应将其添加为子插座

于 2017-01-25T09:34:48.033 回答
5

如果您想避免添加 a 的另一种方法<router-outlet></router-outlet>是将定义路由的方式更改为以下内容:

const routes = [
  { path: '',
    children: [
      {
        path: '',
        component: MailListComponent
      }
    ]
  },
  { path: 'mail', 
    children: [
      {
        path: '',
        component: MailListComponent
      },
      {
        path: 'inbox', 
        component: InboxComponent
      }
    ]
  }
];

这将允许您使用与<router-outlet></router-outlet>上面相同的内容来托管新组件

于 2018-02-09T16:18:07.463 回答