当前位置:  开发笔记 > 编程语言 > 正文

Angular2路由器:错误:无法找到加载'InboxComponent'的主要插座

如何解决《Angular2路由器:错误:无法找到加载'InboxComponent'的主要插座》经验,为你挑选了2个好方法。

我用路由创建了一个简单的应用程序 链接" 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在同一个插座内部渲染,则不应将其添加为儿童



1> PierreDuc..:

您的MailListComponent需求也是如此,因为InboxComponent在路由器配置中定义为子项:

@Component({
  selector: 'app-mail-list',
  template: `
    

Mail list works!

`, styleUrls: ['./mail-list.component.css'] }) export class MailListComponent { constructor() { } }

但是,如果您想InboxComponent在同一个插座内部渲染,则不应将其添加为儿童



2> hasanain..:

如果要避免添加a的另一种方法是将定义路线的方式更改为以下内容:

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

这将允许您使用与上面相同的方法来托管新组件

推荐阅读
罗文彬2502852027
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有