【问题标题】:Error: Uncaught (in promise): Error: Cannot find primary outlet to load 'AppComponent'错误:未捕获(承诺):错误:找不到加载“AppComponent”的主要出口
【发布时间】:2017-06-05 18:59:21
【问题描述】:

我更改了路线,以便我的主页是 appcomponent,现在我收到此错误:

Error: Uncaught (in promise): Error: Cannot find primary outlet to load 'AppComponent'

关于这可能是什么的任何想法?

我的路线:

import {Routes} from "@angular/router";
import {PatientComponent} from "./patient/patient.component";
import {AppComponent} from "./app.component";



export const routes: Routes = [
  {path: '', component: AppComponent},
  {path: 'patient/:id', component: PatientComponent},

];

我的应用组件:

import {Component, OnInit} from '@angular/core';
import {AfdelingService} from './afdeling.service';
import {PatientService} from './patient.service';
import 'rxjs/Rx';
import {Router} from "@angular/router";


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [AfdelingService, PatientService]
})
export class AppComponent implements OnInit {
  errorMessage: string;
  afdeling = [];

  constructor(private afdelingService: AfdelingService, private router: Router) {
  }

  ngOnInit() {
    this.getData()
  }

  goToAfdeling(afdeling) {
    console.log(afdeling.afdelingsNaam);
    this.router.navigate(["/afdelingen", afdeling.afdelingsNaam]);
  }

  getData() {
    this.afdelingService.getAfdelingen()
      .subscribe(
        data => {
          this.afdeling = data;
          console.log(this.afdeling);
        }, error => this.errorMessage = <any> error);


  }
}

【问题讨论】:

  • 您的应用组件应该是您的应用程序外壳,而不应该是路由目标。
  • 在上面的评论中添加...你需要定义router-outlet来显示路由内容...

标签: angular routes angular2-routing


【解决方案1】:

AppComponent 应该是一个路由组件。相反,它应该只是您的应用程序的入口点。这意味着,模板至少应该在它的某些部分包含router outlet,应用程序组件的css选择器应该在你的index.html文件中,AppComponent应该在你的bootstrap列表中声明AppModule

更多信息请见this SO question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2017-06-26
    • 2022-12-19
    • 2023-03-12
    相关资源
    最近更新 更多